Lesson 14, Part 5: Pandas Addition Code¶

  • Reading a .dat file into a Pandas DataFrame
  • Extracting selected rows and columns
index_col -> Makes passed column as index instead of 0, 1, 2, 3 ...¶
In [2]:
import pandas as pd
df=pd.read_csv('c:/Explore/SAS/Lesson14/DEMOGRAPHICS.dat',  index_col='NAME', sep=',')
df.head()
Out[2]:
CONT ID ISO ISONAME region pop popAGR popUrban totalFR AdolescentFPpct AdolescentFPyear AdultLiteracypct MaleSchoolpct FemaleSchoolpct GNI PopPovertypct PopPovertyYear
NAME
BAHAMAS 91 180 44 BAHAMAS AMR 323,063 1.34% 90.00% 2.3 NaN NaN NaN 85.00% 88.00% 16140.0 NaN NaN
BELIZE 91 227 84 BELIZE AMR 269,736 2.14% 48.60% 3.1 12.50% 1998.0 76.90% 98.00% 100.00% 6510.0 NaN NaN
CANADA 91 260 124 CANADA AMR 32,268,243 0.87% 81.10% 1.5 6.50% 1997.0 NaN 100.00% 100.00% 30660.0 NaN NaN
COSTA RICA 91 295 188 COSTA RICA AMR 4,327,228 2.04% 61.70% 2.2 17.40% 1999.0 95.80% 90.00% 91.00% 9530.0 2.00% 2000.0
CUBA 91 300 192 CUBA AMR 11,269,400 0.34% 76.00% 1.6 16.00% 2000.0 99.80% 96.00% 95.00% NaN NaN NaN
To select only the float columns, you can use the select_dtypes method.¶
In [16]:
df.select_dtypes(include = ['float']).head()
Out[16]:
totalFR AdolescentFPyear GNI PopPovertyYear
NAME
BAHAMAS 2.3 NaN 16140.0 NaN
BELIZE 3.1 1998.0 6510.0 NaN
CANADA 1.5 1997.0 30660.0 NaN
COSTA RICA 2.2 1999.0 9530.0 2000.0
CUBA 1.6 2000.0 NaN NaN

To select the sixth row by index position in the df DataFrame, you can pass number 5 to the .iloc indexer.¶

In [19]:
df.iloc[5]
Out[19]:
CONT                                91
ID                                 320
ISO                                214
ISONAME             DOMINICAN REPUBLIC
region                             AMR
pop                          8,894,907
popAGR                           1.34%
popUrban                        60.10%
totalFR                            2.7
AdolescentFPpct                 10.20%
AdolescentFPyear                  1999
AdultLiteracypct                87.70%
MaleSchoolpct                   99.00%
FemaleSchoolpct                 94.00%
GNI                               6750
PopPovertypct                      NaN
PopPovertyYear                    1998
Name: DOMINICAN REPUBLIC, dtype: object

To do the same thing, you can use the index label with the .loc indexer.¶

In [3]:
df.loc['DOMINICAN REPUBLIC']
Out[3]:
CONT                                91
ID                                 320
ISO                                214
ISONAME             DOMINICAN REPUBLIC
region                             AMR
pop                          8,894,907
popAGR                           1.34%
popUrban                        60.10%
totalFR                            2.7
AdolescentFPpct                 10.20%
AdolescentFPyear                  1999
AdultLiteracypct                87.70%
MaleSchoolpct                   99.00%
FemaleSchoolpct                 94.00%
GNI                               6750
PopPovertypct                      NaN
PopPovertyYear                    1998
Name: DOMINICAN REPUBLIC, dtype: object
You can pass a list of NAME values (i.e., index labels) to the .loc indexer to create a subset of the DataFrame.¶
In [22]:
rows=["CHINA", "INDIA"]
df.loc[rows]
Out[22]:
CONT ID ISO ISONAME region pop popAGR popUrban totalFR AdolescentFPpct AdolescentFPyear AdultLiteracypct MaleSchoolpct FemaleSchoolpct GNI PopPovertypct PopPovertyYear
NAME
CHINA 95 280 156 CHINA WPR 1,323,344,591 0.70% 40.50% 1.7 1.00% 2001.0 90.90% NaN NaN 5530.0 16.60% 2001.0
INDIA 95 455 356 INDIA SEAR 1,103,370,802 1.51% 28.70% 3.0 8.10% 1997.0 61.00% 90.00% 85.00% 3100.0 34.70% NaN

Selecting disjointed rows and columns by index position¶

To select particular columns and rows from DataFrame by index position specified in range, you can use the .iloc indexer.

In [34]:
df.iloc[2:5,[5,8,9]]
Out[34]:
pop totalFR AdolescentFPpct
NAME
CANADA 32,268,243 1.5 6.50%
COSTA RICA 4,327,228 2.2 17.40%
CUBA 11,269,400 1.6 16.00%
Select multiple rows & columns by Labels in DataFrame using loc[]¶
In [2]:
df.loc[['CANADA','COSTA RICA', 'CUBA'],['pop', 'totalFR', 'AdolescentFPpct']]
Out[2]:
pop totalFR AdolescentFPpct
NAME
CANADA 32,268,243 1.5 6.50%
COSTA RICA 4,327,228 2.2 17.40%
CUBA 11,269,400 1.6 16.00%