Python KeyError-Don';我不明白为什么
正在尝试获取所有付款方式实例的计数:Python KeyError-Don';我不明白为什么,python,pandas,dictionary,Python,Pandas,Dictionary,正在尝试获取所有付款方式实例的计数: def paymentMethod(self): # Stores unique payment methods as dictionary keys with count of times used as values myDict = {} keyList = list(self.df['Institution'].unique()) for i in keyList:
def paymentMethod(self):
# Stores unique payment methods as dictionary keys with count of times used as values
myDict = {}
keyList = list(self.df['Institution'].unique())
for i in keyList:
count = self.df.groupby(i).count()
myDict.update(i, count)
print(myDict)
这是我的错误:
Traceback (most recent call last):
File "final.py", line 40, in <module>
x.paymentMethod()
File "final.py", line 29, in paymentMethod
count = self.df.groupby(i).count()
File "C:\Users\Sean\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\frame.py", line 6515, in groupby
return DataFrameGroupBy(
File "C:\Users\Sean\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\groupby\groupby.py", line 525, in __init__
grouper, exclusions, obj = get_grouper(
File "C:\Users\Sean\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\groupby\grouper.py", line 786, in get_grouper
raise KeyError(gpr)
KeyError: 'Chase Checking'
回溯(最近一次呼叫最后一次):
文件“final.py”,第40行,在
x、 支付方法(英文)
paymentMethod中第29行的文件“final.py”
count=self.df.groupby(i.count)()
文件“C:\Users\Sean\AppData\Local\Programs\Python\Python38-32\lib\site packages\pandas\core\frame.py”,第6515行,在groupby中
返回DataFrameGroupBy(
文件“C:\Users\Sean\AppData\Local\Programs\Python\Python38-32\lib\site packages\pandas\core\groupby\groupby.py”,第525行,在uu init中__
石斑鱼,排除,obj=get_石斑鱼(
文件“C:\Users\Sean\AppData\Local\Programs\Python\Python38-32\lib\site packages\pandas\core\groupby\grouper.py”,第786行,在get\u grouper中
raise KeyError(探地雷达)
KeyError:“追踪检查”
我真的不明白错误想告诉我什么?如果这是一个nooby问题,很抱歉。
groupby
groups oncolumn names。您的数据框中没有名为'Chase Checking'
的列
你似乎在寻找这个:
def paymentMethod(self):
# Stores unique payment methods as dictionary keys with count of times used as values
myDict = self.df['Institution'].value_counts().to_dict()
print(myDict)
KeyError用于列
Chase Checking
您可以使用计数器
从集合
from collections import Counter
def paymentMethod(self):
count_map = Counter(self.df['Institution'])
return count_map