Basic Manipulation of a Data Frame in Python

Data Frame manipulation in Python

February 23, 2021 · 8 mins read
import pandas as pd
import os
stats = pd.read_csv('D:\\OneDrive - office365hubs.com\\.Python for data science\\Demographic-Data.csv')
stats.columns = ['CountryName','CountryCode','BirthRate','InternetUsers','IncomeGroup']
stats.head()
CountryName CountryCode BirthRate InternetUsers IncomeGroup
0 Aruba ABW 10.244 78.9 High income
1 Afghanistan AFG 35.253 5.9 Low income
2 Angola AGO 45.985 19.1 Upper middle income
3 Albania ALB 12.877 57.2 Upper middle income
4 United Arab Emirates ARE 11.044 88.0 High income
stats[['CountryCode','BirthRate']].head()
CountryCode BirthRate
0 ABW 10.244
1 AFG 35.253
2 AGO 45.985
3 ALB 12.877
4 ARE 11.044
stats[['CountryCode','BirthRate','InternetUsers']].head()
CountryCode BirthRate InternetUsers
0 ABW 10.244 78.9
1 AFG 35.253 5.9
2 AGO 45.985 19.1
3 ALB 12.877 57.2
4 ARE 11.044 88.0
stats[['CountryCode','BirthRate','InternetUsers']][4:8].head()
CountryCode BirthRate InternetUsers
4 ARE 11.044 88.0
5 ARG 17.716 59.9
6 ARM 13.308 41.9
7 ATG 16.447 63.4
#mathematical operations:
result = stats.BirthRate * stats.InternetUsers
result.head()
0    808.2516
1    207.9927
2    878.3135
3    736.5644
4    971.8720
dtype: float64
# Add a column to your data frame
stats['MyCalc'] = stats.BirthRate * stats.InternetUsers
stats.head()
CountryName CountryCode BirthRate InternetUsers IncomeGroup MyCalc
0 Aruba ABW 10.244 78.9 High income 808.2516
1 Afghanistan AFG 35.253 5.9 Low income 207.9927
2 Angola AGO 45.985 19.1 Upper middle income 878.3135
3 Albania ALB 12.877 57.2 Upper middle income 736.5644
4 United Arab Emirates ARE 11.044 88.0 High income 971.8720
#Removing a column
stats.head()
CountryName CountryCode BirthRate InternetUsers IncomeGroup MyCalc
0 Aruba ABW 10.244 78.9 High income 808.2516
1 Afghanistan AFG 35.253 5.9 Low income 207.9927
2 Angola AGO 45.985 19.1 Upper middle income 878.3135
3 Albania ALB 12.877 57.2 Upper middle income 736.5644
4 United Arab Emirates ARE 11.044 88.0 High income 971.8720
stats.drop('MyCalc', 1)
CountryName CountryCode BirthRate InternetUsers IncomeGroup
0 Aruba ABW 10.244 78.9 High income
1 Afghanistan AFG 35.253 5.9 Low income
2 Angola AGO 45.985 19.1 Upper middle income
3 Albania ALB 12.877 57.2 Upper middle income
4 United Arab Emirates ARE 11.044 88.0 High income
... ... ... ... ... ...
190 Yemen, Rep. YEM 32.947 20.0 Lower middle income
191 South Africa ZAF 20.850 46.5 Upper middle income
192 Congo, Dem. Rep. COD 42.394 2.2 Low income
193 Zambia ZMB 40.471 15.4 Lower middle income
194 Zimbabwe ZWE 35.715 18.5 Low income

195 rows × 5 columns

stats = stats.drop('MyCalc', 1)
stats
CountryName CountryCode BirthRate InternetUsers IncomeGroup
0 Aruba ABW 10.244 78.9 High income
1 Afghanistan AFG 35.253 5.9 Low income
2 Angola AGO 45.985 19.1 Upper middle income
3 Albania ALB 12.877 57.2 Upper middle income
4 United Arab Emirates ARE 11.044 88.0 High income
... ... ... ... ... ...
190 Yemen, Rep. YEM 32.947 20.0 Lower middle income
191 South Africa ZAF 20.850 46.5 Upper middle income
192 Congo, Dem. Rep. COD 42.394 2.2 Low income
193 Zambia ZMB 40.471 15.4 Lower middle income
194 Zimbabwe ZWE 35.715 18.5 Low income

195 rows × 5 columns