简体   繁体   中英

for loop repeats 17 times

I have the following code, but when I execute it, it prints the age_groups_list 17 times,

Any idea why?

import pandas as pd
file = pd.read_csv(r"file location")
age_groups_list = []
for var in file[1:]:
    age = file.iloc[:, 10]
    age_groups_list.append(age)
print(age_groups_list)

the idea is that I have a csv file with 16,000 (+) rows and 20 columns, I am picking the age group from index 10, adding it to a list and then print the list, however when printing the list, it does it for 17 time, this image shows the end of the printing output. 在此处输入图像描述 Any idea what am I doing wrong here? thanks

file.iloc[:,10] already gives you all the data you need the loop is useless what you see is actually a list of lists. change it to this:

import pandas as pd
file = pd.read_csv(r"file location")
age_groups_list = file.iloc[:, 10]
print(age_groups_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM