简体   繁体   English

for 循环重复 17 次

[英]for loop repeats 17 times

I have the following code, but when I execute it, it prints the age_groups_list 17 times,我有以下代码,但是当我执行它时,它会打印 age_groups_list 17 次,

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.这个想法是我有一个 csv 文件,其中包含 16,000 (+) 行和 20 列,我从索引 10 中选择年龄组,将其添加到列表中,然后打印列表,但是在打印列表时,它是为17的时候,这张图片显示打印结束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. file.iloc[:,10] 已经为您提供了您需要的所有数据循环是无用的您看到的实际上是一个列表列表。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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