Home > database >  Dropping multiples rows based on list of column values
Dropping multiples rows based on list of column values

Time:11-17

I am working on a World Bank dataset of Co2 Emission and GDP. I want to remove values of non countries from the dataframe.

I tried using negation and reset_index as follow. But the rows are not getting removed. I want the rows to be removed where the country name in the column Country Name is from the list non_countries.

df_indicator  = df_indicator[~df_indicator['Country Name'].isin(non_countries)]
df_indicator.reset_index(inplace=True, drop=True)

The list of non countries, which I want to drop from dataframe is as follows:

non_countries=['World','High income','OECD members','Post-demographic dividend','IDA & IBRD total','Low & middle income','Middle income','IBRD only', 'East Asia & Pacific','Europe & Central Asia','North America','Upper middle income','Late-demographic dividend','European Union','East Asia & Pacific (excluding high income)',
 'East Asia & Pacific (IDA & IBRD countries)','Euro area','Early-demographic dividend','Lower middle income','Latin America & Caribbean','Latin America & the Caribbean (IDA & IBRD countries)','Latin America & Caribbean (excluding high income)','Europe & Central Asia (IDA & IBRD countries)','Middle East & North Africa','Europe & Central Asia (excluding high income)','South Asia (IDA & IBRD)','South Asia','Arab World','IDA total','Sub-Saharan Africa','Sub-Saharan Africa (IDA & IBRD countries)','Sub-Saharan Africa (excluding high income)','Middle East & North Africa (excluding high income)','Middle East & North Africa (IDA & IBRD countries)','Central Europe and the Baltics','Pre-demographic dividend','IDA only','Least developed countries: UN classification','IDA blend','Fragile and conflict affected situations','Heavily indebted poor countries (HIPC)','Low income', 'Small states','Other small states','Not classified','Caribbean small states','Pacific island small states']

Which is the most efficient method to drop such rows?

CodePudding user response:

Try this:

df_indicator = df_indicator.loc[~df_indicator['Country Name'].isin(non_countries)]
  • Related