python - KeyError - 不明白为什么
问题描述
尝试获取所有付款方式实例的计数:
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
列名上的组。您的数据框没有名为'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)
推荐阅读
- c++ - 在 CLion/Mingw 上未定义对“curl_easy_init”的引用
- powershell - 如何使用 WIQL 从 RESTful API 获取数据,但似乎无法正常工作
- javascript - 使用 dispatch() 时没有调用减速器
- r - 从分号分隔的电子邮件向量中提取具有特定域的电子邮件
- android - 如何从文本视图字段上的数据输入发送文本消息?
- protocol-buffers - 协议缓冲区:只读字段?
- javascript - 移动(或小型设备)视图上的 owl-stage 宽度大于屏幕
- ios - UIView 在滚动时从 UICollectionView 的底部分离
- java - 如何编写一个将字符串中每个数字的数字相加的java方法
- amazon-web-services - 我是否需要为创建和保留 AWS S3 存储桶付费?