简体   繁体   English

Python-遍历CSV文件中的特定列

[英]Python - Looping through specific column in CSV file

I currently have my csv file displaying on python: 我目前在python上显示我的csv文件:

df = pd.read_csv("Desktop\Assignment\World Cup 2018.csv")
df.head()

Here I can see that my data has been opened and the unneeded columns have been removed. 在这里,我可以看到我的数据已被打开,不需要的列已被删除。 Now I want to use some variables named CounterVal1 (and so on) to count the amount of times a formation appears in a row. 现在,我想使用一些名为CounterVal1的变量(以此类推)来计算某行连续出现的次数。

for i in enumerate(df['home_formation']):
if i == '4-2-3-1':
    counterVal1 += 1
elif i == '4-1-4-1':
      counterVal2 += 1

performance = [counterVal1,counterVal2,counterVal3,counterVal4,counterVal5,counterVal6]

plt.bar(y_pos, performance, align='center', alpha=0.8)

However I have printed off one of these values and it appears that the data is not being searched through as show above. 但是,我已打印出这些值之一,并且似乎没有如上所示搜索数据。

My question: How do I take in the data from the CSV file, take just the column related to the formation and loop through it? 我的问题:如何从CSV文件中获取数据,仅获取与格式相关的列并循环遍历?

You haven't really provided much here. 您在这里确实没有提供太多信息。 So this is just guess work. 因此,这只是猜测。

Based on your comment I'm guessing this is what you want : 根据您的评论,我猜这就是您想要的:

df['home-formation'].value_counts().plot('bar')

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

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