我在youtube上做了一个练习,尝试在csv数据中获取字典计数器。 代码如下:
import pandas as pd
from collections import Counter
URL = 'https://raw.githubusercontent.com/CoreyMSchafer/code_snippets/master/Python/Matplotlib/02-BarCharts/data.csv'
data = pd.read_csv(url)
df = pd.DataFrame.to_dict(data)
language_counter = Counter()
for i in df:
language_counter.update(df['LanguagesWorkedWith'][i].split(';'))
print(language_counter)
知道哪里出了问题吗?这是显示的错误:
Traceback (most recent call last):
File "C:/Users/jong5/PycharmProjects/learning/matplotlib/matplotlib-Bar.py", line 14, in <module>
language_counter.update(df['LanguagesWorkedWith'][i].split(';'))
KeyError: 'Responder_id'
“Responder_id”是第一个列名。 谢谢你的帮助,谢谢
谢谢你的回复,在玩过之后,我终于找到了办法。对不起,如果我不清楚这些问题。 以下是修改后的代码:
没有熊猫
输出
不需要在列上迭代时使用
to_dict
。试试for column_value in data_frame['column_name']
相关问题 更多 >
编程相关推荐