세무사 서찬영 세무회계 사무소

Pandas Dataframe 에서 seriese 선택하는 방법

Q02

pandas 데이터 프레임에서 시리즈를 얻어 내는 방법

pandas 에는 두가지 기본적인 object type 이 있다

하나는 dataframe 이고

raw 와 column 으로 이루어진 table 형태의 data

하나는 series 이다.

dataframe 의 각 column 은 pandas 의 series 로 이루어져있다.
In [3]:
import pandas as pd
In [4]:
ufo = pd.read_csv('http://bit.ly/uforeports')
In [5]:
type(ufo)
Out[5]:
pandas.core.frame.DataFrame
In [6]:
ufo.head()
Out[6]:
City Colors Reported Shape Reported State Time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [7]:
ufo['City']
Out[7]:
0                      Ithaca
1                 Willingboro
2                     Holyoke
3                     Abilene
4        New York Worlds Fair
5                 Valley City
6                 Crater Lake
7                        Alma
8                     Eklutna
9                     Hubbard
10                    Fontana
11                   Waterloo
12                     Belton
13                     Keokuk
14                  Ludington
15                Forest Home
16                Los Angeles
17                  Hapeville
18                     Oneida
19                 Bering Sea
20                   Nebraska
21                        NaN
22                        NaN
23                  Owensboro
24                 Wilderness
25                  San Diego
26                 Wilderness
27                     Clovis
28                 Los Alamos
29               Ft. Duschene
                 ...         
18211                 Holyoke
18212                  Carson
18213                Pasadena
18214                  Austin
18215                El Campo
18216            Garden Grove
18217           Berthoud Pass
18218              Sisterdale
18219            Garden Grove
18220             Shasta Lake
18221                Franklin
18222          Albrightsville
18223              Greenville
18224                 Eufaula
18225             Simi Valley
18226           San Francisco
18227           San Francisco
18228              Kingsville
18229                 Chicago
18230             Pismo Beach
18231             Pismo Beach
18232                    Lodi
18233               Anchorage
18234                Capitola
18235          Fountain Hills
18236              Grant Park
18237             Spirit Lake
18238             Eagle River
18239             Eagle River
18240                    Ybor
Name: City, Length: 18241, dtype: object
In [9]:
type(ufo['City'])
Out[9]:
pandas.core.series.Series
In [10]:
# ['City'] 대신 .City 를 사용해도 된다
type(ufo.City)
Out[10]:
pandas.core.series.Series
In [11]:
'ab'+'cd'
Out[11]:
'abcd'

새로운 컬럼 추가 하기

In [14]:
ufo['Location'] = ufo.City +','+ ufo.State
In [15]:
ufo.head()
Out[15]:
City Colors Reported Shape Reported State Time Location
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00 Ithaca,NY
1 Willingboro NaN OTHER NJ 6/30/1930 20:00 Willingboro,NJ
2 Holyoke NaN OVAL CO 2/15/1931 14:00 Holyoke,CO
3 Abilene NaN DISK KS 6/1/1931 13:00 Abilene,KS
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00 New York Worlds Fair,NY

댓글

Designed by JB FACTORY