Python Forum

Full Version: Populate the new lists by looping over the original lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
have dataframe gapminder and every column is organized as separate list using code as below:

country = df.country.to_list()
continent = df.continent.to_list()
year = df.year.to_list()
I have created an empty list country_1990 and I want to populate it with data from year 1990 by looping over the original list country

How can I perform this task? Tried this:

for i in range(len(df.country)): 
        if df.loc[df['year']== 1990 :
            country_1990.append(df.country[i])
print(country_1990)
to check at each iteration of the loop if the associated record is from 1990 but not working
Thx
your code does not have as many ] as it has [ so they are out of balance.  i cannot figure out just what you are trying to do.  can you fix it and try post it again (with tags this time).