Thursday, February 21, 2019

How do I select a pandas Series from a DataFrame

pandas2
In [1]:
import pandas as pd
In [2]:
ufo = pd.read_csv('http://bit.ly/uforeports')
In [3]:
type(ufo)
Out[3]:
pandas.core.frame.DataFrame
In [4]:
ufo.head()
Out[4]:
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]:
ufo.City
Out[10]:
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 [11]:
ufo.Colors Reported
  File "<ipython-input-11-823f97112488>", line 1
    ufo.Colors Reported
                      ^
SyntaxError: invalid syntax
In [13]:
ufo['Colors Reported']
Out[13]:
0           NaN
1           NaN
2           NaN
3           NaN
4           NaN
5           NaN
6           NaN
7           NaN
8           NaN
9           NaN
10          NaN
11          NaN
12          RED
13          NaN
14          NaN
15          NaN
16          NaN
17          NaN
18          NaN
19          RED
20          NaN
21          NaN
22          NaN
23          NaN
24          NaN
25          NaN
26          NaN
27          NaN
28          NaN
29          NaN
          ...  
18211       NaN
18212       NaN
18213     GREEN
18214       NaN
18215       NaN
18216    ORANGE
18217       NaN
18218       NaN
18219       NaN
18220      BLUE
18221       NaN
18222       NaN
18223       NaN
18224       NaN
18225       NaN
18226       NaN
18227       NaN
18228       NaN
18229       NaN
18230       NaN
18231       NaN
18232       NaN
18233       RED
18234       NaN
18235       NaN
18236       NaN
18237       NaN
18238       NaN
18239       RED
18240       NaN
Name: Colors Reported, Length: 18241, dtype: object
In [14]:
ufo.shape
Out[14]:
(18241, 5)
In [15]:
'ab' + 'cd'
Out[15]:
'abcd'
In [16]:
ufo.City + ufo.State
Out[16]:
0                      IthacaNY
1                 WillingboroNJ
2                     HolyokeCO
3                     AbileneKS
4        New York Worlds FairNY
5                 Valley CityND
6                 Crater LakeCA
7                        AlmaMI
8                     EklutnaAK
9                     HubbardOR
10                    FontanaCA
11                   WaterlooAL
12                     BeltonSC
13                     KeokukIA
14                  LudingtonMI
15                Forest HomeCA
16                Los AngelesCA
17                  HapevilleGA
18                     OneidaTN
19                 Bering SeaAK
20                   NebraskaNE
21                          NaN
22                          NaN
23                  OwensboroKY
24                 WildernessWV
25                  San DiegoCA
26                 WildernessWV
27                     ClovisNM
28                 Los AlamosNM
29               Ft. DuscheneUT
                  ...          
18211                 HolyokeMA
18212                  CarsonCA
18213                PasadenaCA
18214                  AustinTX
18215                El CampoTX
18216            Garden GroveCA
18217           Berthoud PassCO
18218              SisterdaleTX
18219            Garden GroveCA
18220             Shasta LakeCA
18221                FranklinNH
18222          AlbrightsvillePA
18223              GreenvilleSC
18224                 EufaulaOK
18225             Simi ValleyCA
18226           San FranciscoCA
18227           San FranciscoCA
18228              KingsvilleTX
18229                 ChicagoIL
18230             Pismo BeachCA
18231             Pismo BeachCA
18232                    LodiWI
18233               AnchorageAK
18234                CapitolaCA
18235          Fountain HillsAZ
18236              Grant ParkIL
18237             Spirit LakeIA
18238             Eagle RiverWI
18239             Eagle RiverWI
18240                    YborFL
Length: 18241, dtype: object
In [19]:
ufo.City +', ' + ufo.State
Out[19]:
0                      Ithaca, NY
1                 Willingboro, NJ
2                     Holyoke, CO
3                     Abilene, KS
4        New York Worlds Fair, NY
5                 Valley City, ND
6                 Crater Lake, CA
7                        Alma, MI
8                     Eklutna, AK
9                     Hubbard, OR
10                    Fontana, CA
11                   Waterloo, AL
12                     Belton, SC
13                     Keokuk, IA
14                  Ludington, MI
15                Forest Home, CA
16                Los Angeles, CA
17                  Hapeville, GA
18                     Oneida, TN
19                 Bering Sea, AK
20                   Nebraska, NE
21                            NaN
22                            NaN
23                  Owensboro, KY
24                 Wilderness, WV
25                  San Diego, CA
26                 Wilderness, WV
27                     Clovis, NM
28                 Los Alamos, NM
29               Ft. Duschene, UT
                   ...           
18211                 Holyoke, MA
18212                  Carson, CA
18213                Pasadena, CA
18214                  Austin, TX
18215                El Campo, TX
18216            Garden Grove, CA
18217           Berthoud Pass, CO
18218              Sisterdale, TX
18219            Garden Grove, CA
18220             Shasta Lake, CA
18221                Franklin, NH
18222          Albrightsville, PA
18223              Greenville, SC
18224                 Eufaula, OK
18225             Simi Valley, CA
18226           San Francisco, CA
18227           San Francisco, CA
18228              Kingsville, TX
18229                 Chicago, IL
18230             Pismo Beach, CA
18231             Pismo Beach, CA
18232                    Lodi, WI
18233               Anchorage, AK
18234                Capitola, CA
18235          Fountain Hills, AZ
18236              Grant Park, IL
18237             Spirit Lake, IA
18238             Eagle River, WI
18239             Eagle River, WI
18240                    Ybor, FL
Length: 18241, dtype: object
In [23]:
ufo.Location = ufo.City +', ' + ufo.State 
# Note use ufo['Location'] insted of ufo.Location
In [21]:
ufo.Location
Out[21]:
0                      Ithaca, NY
1                 Willingboro, NJ
2                     Holyoke, CO
3                     Abilene, KS
4        New York Worlds Fair, NY
5                 Valley City, ND
6                 Crater Lake, CA
7                        Alma, MI
8                     Eklutna, AK
9                     Hubbard, OR
10                    Fontana, CA
11                   Waterloo, AL
12                     Belton, SC
13                     Keokuk, IA
14                  Ludington, MI
15                Forest Home, CA
16                Los Angeles, CA
17                  Hapeville, GA
18                     Oneida, TN
19                 Bering Sea, AK
20                   Nebraska, NE
21                            NaN
22                            NaN
23                  Owensboro, KY
24                 Wilderness, WV
25                  San Diego, CA
26                 Wilderness, WV
27                     Clovis, NM
28                 Los Alamos, NM
29               Ft. Duschene, UT
                   ...           
18211                 Holyoke, MA
18212                  Carson, CA
18213                Pasadena, CA
18214                  Austin, TX
18215                El Campo, TX
18216            Garden Grove, CA
18217           Berthoud Pass, CO
18218              Sisterdale, TX
18219            Garden Grove, CA
18220             Shasta Lake, CA
18221                Franklin, NH
18222          Albrightsville, PA
18223              Greenville, SC
18224                 Eufaula, OK
18225             Simi Valley, CA
18226           San Francisco, CA
18227           San Francisco, CA
18228              Kingsville, TX
18229                 Chicago, IL
18230             Pismo Beach, CA
18231             Pismo Beach, CA
18232                    Lodi, WI
18233               Anchorage, AK
18234                Capitola, CA
18235          Fountain Hills, AZ
18236              Grant Park, IL
18237             Spirit Lake, IA
18238             Eagle River, WI
18239             Eagle River, WI
18240                    Ybor, FL
Length: 18241, dtype: object
In [ ]:
 

No comments:

Post a Comment