Python Forum
Populate the new lists by looping over the original lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populate the new lists by looping over the original lists
#1
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
Reply
#2
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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 312 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  problem with print lists MarekGwozdz 4 695 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,565 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 771 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 764 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 819 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,441 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 748 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,284 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 779 Feb-28-2023, 10:55 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020