[英]KeyError - Don't understand why
嘗試獲取所有付款方式實例的計數:
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'
我真的不明白錯誤試圖告訴我什么? 對不起,如果這是一個nooby問題。
groupby
列名稱上的組。 您的 dataframe 沒有名為'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
您可以使用來自collections
的Counter
生成計數 map
from collections import Counter
def paymentMethod(self):
count_map = Counter(self.df['Institution'])
return count_map
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.