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
  Appending lists into lists from function is not working for me BoredBannana 2 1,128 Oct-25-2024, 05:18 PM
Last Post: deanhystad
  Nested Lists & Dictionaries Hudjefa 5 1,302 Sep-23-2024, 08:20 PM
Last Post: DeaD_EyE
Sad The issue with compare 2D lists przonak007 18 3,239 Jul-17-2024, 07:31 AM
Last Post: Pedroski55
  Compare lists w_i_k_i_d 6 1,620 May-23-2024, 07:23 PM
Last Post: deanhystad
Question Using Lists as Dictionary Values bfallert 8 2,175 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  problem with print lists MarekGwozdz 4 1,614 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 3,199 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 2,141 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 1,730 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 1,607 Jul-01-2023, 09:19 PM
Last Post: ICanIBB

Forum Jump:

User Panel Messages

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