Plotting a large data set while filtering for a year that is not a variable in that data set

Viewed 17

Really easy question from a starting python programmer, but I am have been fighting this for two days now.

I want to plot life expectancy vs gdp in a scatter plot. This comes from a huge 60000 row data set containing the years 1950 until 2018. For this specific scatterplot I am only interested in 2018. How do I filter the df for this specific task without deleting data.

As shown below the variable year is not in my scatterplot, but I would like to filter it.

This is the code I have now, I have tried everything like df.loc, etc.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('GDP_LE_2018.csv')

le = df['Life expectancy']
gdp = df['GDP per capita']
year = df['Year']
year1 = df[df['Year'] == 2018]

plt.scatter(gdp, le)

plt.show()

This is the data set

 Entity      Code  ...  Population (historical estimates)  Continent
0     Abkhazia  OWID_ABK  ...                                NaN       Asia
1  Afghanistan       AFG  ...                          7752117.0        NaN
2  Afghanistan       AFG  ...                          7840151.0        NaN
3  Afghanistan       AFG  ...                          7935996.0        NaN
4  Afghanistan       AFG  ...                          8039684.0        NaN

Help would be so much appreciated

1 Answers
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('GDP_LE_2018.csv')
df = pd.DataFrame(df)
df = df.query('Year > == 2018')
le = df['Life expectancy']
gdp = df['GDP per capita']

plt.scatter(gdp, le)

plt.show()
  1. convert Dataframe.
  2. filter(query).
Related