Python-c07-Filtering-and-Ordering

1
import pandas as pd
1
df = pd.read_csv("world_population.csv")
1
df[df["Rank"] < 10]

Rank CCA3 Country Capital Continent 2022 Population 2020 Population 2015 Population 2010 Population 2000 Population 1990 Population 1980 Population 1970 Population Area (km²) Density (per km²) Growth Rate World Population Percentage
16 8 BGD Bangladesh Dhaka Asia 1.711864e+08 1.674210e+08 1.578300e+08 1.483911e+08 1.291933e+08 1.071477e+08 83929765.0 67541860.0 147570.0 1160.0350 1.0108 2.15
27 7 BRA Brazil Brasilia South America 2.153135e+08 2.131963e+08 2.051882e+08 1.963535e+08 1.758737e+08 1.507064e+08 122288383.0 96369875.0 8515767.0 25.2841 1.0046 2.70
41 1 CHN China Beijing Asia 1.425887e+09 1.424930e+09 1.393715e+09 1.348191e+09 1.264099e+09 1.153704e+09 982372466.0 822534450.0 9706961.0 146.8933 1.0000 17.88
92 2 IND India New Delhi Asia 1.417173e+09 1.396387e+09 1.322867e+09 1.240614e+09 1.059634e+09 NaN NaN 557501301.0 3287590.0 431.0675 1.0068 17.77
93 4 IDN Indonesia Jakarta Asia 2.755013e+08 2.718580e+08 2.590920e+08 2.440162e+08 2.140724e+08 1.821599e+08 148177096.0 115228394.0 1904569.0 144.6529 1.0064 3.45
149 6 NGA Nigeria Abuja Africa 2.185412e+08 2.083274e+08 1.839958e+08 1.609529e+08 1.228520e+08 9.521426e+07 72951439.0 55569264.0 923768.0 236.5759 1.0241 2.74
156 5 PAK Pakistan Islamabad Asia 2.358249e+08 2.271967e+08 2.109693e+08 1.944545e+08 1.543699e+08 1.154141e+08 80624057.0 59290872.0 881912.0 267.4018 1.0191 2.96
171 9 RUS Russia Moscow Europe 1.447133e+08 1.456173e+08 1.446684e+08 1.432426e+08 1.468448e+08 1.480057e+08 138257420.0 130093010.0 17098242.0 8.4636 0.9973 1.81
221 3 USA United States Washington, D.C. North America 3.382899e+08 3.359420e+08 3.246078e+08 3.111828e+08 2.823986e+08 2.480837e+08 223140018.0 200328340.0 9372610.0 36.0935 1.0038 4.24
1
2
specific_countries = ['Bangladesh', 'Brazil']
df[df['Country'].isin(specific_countries)]

Rank CCA3 Country Capital Continent 2022 Population 2020 Population 2015 Population 2010 Population 2000 Population 1990 Population 1980 Population 1970 Population Area (km²) Density (per km²) Growth Rate World Population Percentage
16 8 BGD Bangladesh Dhaka Asia 171186372.0 167420951.0 157830000.0 148391139.0 129193327.0 107147651.0 83929765.0 67541860.0 147570.0 1160.0350 1.0108 2.15
27 7 BRA Brazil Brasilia South America 215313498.0 213196304.0 205188205.0 196353492.0 175873720.0 150706446.0 122288383.0 96369875.0 8515767.0 25.2841 1.0046 2.70
1
2
# filter country which contains United
df[df['Country'].str.contains('United')]

Rank CCA3 Country Capital Continent 2022 Population 2020 Population 2015 Population 2010 Population 2000 Population 1990 Population 1980 Population 1970 Population Area (km²) Density (per km²) Growth Rate World Population Percentage
219 97 ARE United Arab Emirates Abu Dhabi Asia 9441129.0 9287289.0 8916899.0 8481771.0 3275333.0 1900151.0 1014048.0 298084.0 83600.0 112.9322 1.0081 0.12
220 21 GBR United Kingdom London Europe 67508936.0 67059474.0 65224364.0 62760039.0 58850043.0 57210442.0 56326328.0 55650166.0 242900.0 277.9289 1.0034 0.85
221 3 USA United States Washington, D.C. North America 338289857.0 335942003.0 324607776.0 311182845.0 282398554.0 248083732.0 223140018.0 200328340.0 9372610.0 36.0935 1.0038 4.24
222 200 VIR United States Virgin Islands Charlotte Amalie North America 99465.0 100442.0 102803.0 106142.0 108185.0 100685.0 96640.0 63446.0 347.0 286.6427 0.9937 0.00
1
2
3
4
# set index to country
df2 = df.set_index('Country')
# show only the columns Contient and CCA3
df2.filter(items = ['Continent', 'CCA3'])

Continent CCA3
Country
Afghanistan Asia AFG
Albania Europe ALB
Algeria Africa DZA
American Samoa Oceania ASM
Andorra Europe AND
... ... ...
Wallis and Futuna Oceania WLF
Western Sahara Africa ESH
Yemen Asia YEM
Zambia Africa ZMB
Zimbabwe Africa ZWE

234 rows × 2 columns

1
2
3
# axis= is the vertical axis and axis=1 is the horizontal axis
# The following returns 0 result as 'Continent', 'CCA3' do not exist in the vertical axis which is Country
df2.filter(items=['Continent', 'CCA3'], axis=0)

Rank CCA3 Capital Continent 2022 Population 2020 Population 2015 Population 2010 Population 2000 Population 1990 Population 1980 Population 1970 Population Area (km²) Density (per km²) Growth Rate World Population Percentage
1
2
# but axis=1 exists as 'Continent', 'CCA3' exist in the horizontal axis
df2.filter(items=['Continent', 'CCA3'], axis=1)

Continent CCA3
Country
Afghanistan Asia AFG
Albania Europe ALB
Algeria Africa DZA
American Samoa Oceania ASM
Andorra Europe AND
... ... ...
Wallis and Futuna Oceania WLF
Western Sahara Africa ESH
Yemen Asia YEM
Zambia Africa ZMB
Zimbabwe Africa ZWE

234 rows × 2 columns

1
2
# This will return all the countries which contains "United"
df2.filter(like='United', axis=0)

Rank CCA3 Capital Continent 2022 Population 2020 Population 2015 Population 2010 Population 2000 Population 1990 Population 1980 Population 1970 Population Area (km²) Density (per km²) Growth Rate World Population Percentage
Country
United Arab Emirates 97 ARE Abu Dhabi Asia 9441129.0 9287289.0 8916899.0 8481771.0 3275333.0 1900151.0 1014048.0 298084.0 83600.0 112.9322 1.0081 0.12
United Kingdom 21 GBR London Europe 67508936.0 67059474.0 65224364.0 62760039.0 58850043.0 57210442.0 56326328.0 55650166.0 242900.0 277.9289 1.0034 0.85
United States 3 USA Washington, D.C. North America 338289857.0 335942003.0 324607776.0 311182845.0 282398554.0 248083732.0 223140018.0 200328340.0 9372610.0 36.0935 1.0038 4.24
United States Virgin Islands 200 VIR Charlotte Amalie North America 99465.0 100442.0 102803.0 106142.0 108185.0 100685.0 96640.0 63446.0 347.0 286.6427 0.9937 0.00
1
df2.loc['United States']
Rank                                          3
CCA3                                        USA
Capital                        Washington, D.C.
Continent                         North America
2022 Population                     338289857.0
2020 Population                     335942003.0
2015 Population                     324607776.0
2010 Population                     311182845.0
2000 Population                     282398554.0
1990 Population                     248083732.0
1980 Population                     223140018.0
1970 Population                     200328340.0
Area (km²)                            9372610.0
Density (per km²)                       36.0935
Growth Rate                              1.0038
World Population Percentage                4.24
Name: United States, dtype: object
1
df2.iloc[3]
Rank                                 213
CCA3                                 ASM
Capital                        Pago Pago
Continent                        Oceania
2022 Population                  44273.0
2020 Population                  46189.0
2015 Population                  51368.0
2010 Population                  54849.0
2000 Population                  58230.0
1990 Population                  47818.0
1980 Population                  32886.0
1970 Population                  27075.0
Area (km²)                         199.0
Density (per km²)               222.4774
Growth Rate                       0.9831
World Population Percentage          0.0
Name: American Samoa, dtype: object
1
2
# sort by Rank and Country in ascending
df[df['Rank'] < 10].sort_values(by=['Rank', 'Country'], ascending=True)

Rank CCA3 Country Capital Continent 2022 Population 2020 Population 2015 Population 2010 Population 2000 Population 1990 Population 1980 Population 1970 Population Area (km²) Density (per km²) Growth Rate World Population Percentage
41 1 CHN China Beijing Asia 1.425887e+09 1.424930e+09 1.393715e+09 1.348191e+09 1.264099e+09 1.153704e+09 982372466.0 822534450.0 9706961.0 146.8933 1.0000 17.88
92 2 IND India New Delhi Asia 1.417173e+09 1.396387e+09 1.322867e+09 1.240614e+09 1.059634e+09 NaN NaN 557501301.0 3287590.0 431.0675 1.0068 17.77
221 3 USA United States Washington, D.C. North America 3.382899e+08 3.359420e+08 3.246078e+08 3.111828e+08 2.823986e+08 2.480837e+08 223140018.0 200328340.0 9372610.0 36.0935 1.0038 4.24
93 4 IDN Indonesia Jakarta Asia 2.755013e+08 2.718580e+08 2.590920e+08 2.440162e+08 2.140724e+08 1.821599e+08 148177096.0 115228394.0 1904569.0 144.6529 1.0064 3.45
156 5 PAK Pakistan Islamabad Asia 2.358249e+08 2.271967e+08 2.109693e+08 1.944545e+08 1.543699e+08 1.154141e+08 80624057.0 59290872.0 881912.0 267.4018 1.0191 2.96
149 6 NGA Nigeria Abuja Africa 2.185412e+08 2.083274e+08 1.839958e+08 1.609529e+08 1.228520e+08 9.521426e+07 72951439.0 55569264.0 923768.0 236.5759 1.0241 2.74
27 7 BRA Brazil Brasilia South America 2.153135e+08 2.131963e+08 2.051882e+08 1.963535e+08 1.758737e+08 1.507064e+08 122288383.0 96369875.0 8515767.0 25.2841 1.0046 2.70
16 8 BGD Bangladesh Dhaka Asia 1.711864e+08 1.674210e+08 1.578300e+08 1.483911e+08 1.291933e+08 1.071477e+08 83929765.0 67541860.0 147570.0 1160.0350 1.0108 2.15
171 9 RUS Russia Moscow Europe 1.447133e+08 1.456173e+08 1.446684e+08 1.432426e+08 1.468448e+08 1.480057e+08 138257420.0 130093010.0 17098242.0 8.4636 0.9973 1.81
1
2
# sort by Rank in ascending and Country in descending
df[df['Rank'] < 10].sort_values(by=['Rank', 'Country'], ascending=[True, False])

Rank CCA3 Country Capital Continent 2022 Population 2020 Population 2015 Population 2010 Population 2000 Population 1990 Population 1980 Population 1970 Population Area (km²) Density (per km²) Growth Rate World Population Percentage
41 1 CHN China Beijing Asia 1.425887e+09 1.424930e+09 1.393715e+09 1.348191e+09 1.264099e+09 1.153704e+09 982372466.0 822534450.0 9706961.0 146.8933 1.0000 17.88
92 2 IND India New Delhi Asia 1.417173e+09 1.396387e+09 1.322867e+09 1.240614e+09 1.059634e+09 NaN NaN 557501301.0 3287590.0 431.0675 1.0068 17.77
221 3 USA United States Washington, D.C. North America 3.382899e+08 3.359420e+08 3.246078e+08 3.111828e+08 2.823986e+08 2.480837e+08 223140018.0 200328340.0 9372610.0 36.0935 1.0038 4.24
93 4 IDN Indonesia Jakarta Asia 2.755013e+08 2.718580e+08 2.590920e+08 2.440162e+08 2.140724e+08 1.821599e+08 148177096.0 115228394.0 1904569.0 144.6529 1.0064 3.45
156 5 PAK Pakistan Islamabad Asia 2.358249e+08 2.271967e+08 2.109693e+08 1.944545e+08 1.543699e+08 1.154141e+08 80624057.0 59290872.0 881912.0 267.4018 1.0191 2.96
149 6 NGA Nigeria Abuja Africa 2.185412e+08 2.083274e+08 1.839958e+08 1.609529e+08 1.228520e+08 9.521426e+07 72951439.0 55569264.0 923768.0 236.5759 1.0241 2.74
27 7 BRA Brazil Brasilia South America 2.153135e+08 2.131963e+08 2.051882e+08 1.963535e+08 1.758737e+08 1.507064e+08 122288383.0 96369875.0 8515767.0 25.2841 1.0046 2.70
16 8 BGD Bangladesh Dhaka Asia 1.711864e+08 1.674210e+08 1.578300e+08 1.483911e+08 1.291933e+08 1.071477e+08 83929765.0 67541860.0 147570.0 1160.0350 1.0108 2.15
171 9 RUS Russia Moscow Europe 1.447133e+08 1.456173e+08 1.446684e+08 1.432426e+08 1.468448e+08 1.480057e+08 138257420.0 130093010.0 17098242.0 8.4636 0.9973 1.81

PS:
world_population.csv