MjpDm 2023-06-14 23:25 采纳率: 66.7%
浏览 24
已结题

Python Pandas groupby 后 通过index获取值报错KeyErro

Python Pandas groupby 后 通过index获取值报错KeyErro

学习pandas时,发现通过index获取groupby操作后的数据报错keyerror,大佬们帮忙看下是什么问题

源代码


import pandas as pd
def pd_account(list_agg,list_group,model):
    filename = "datafile/home_account.xlsx"
    df = pd.read_excel(filename)
    df.columns = ['y','m','d','lsfx','lsje','zffs','lslb','bz','fqr']
    list_col = list_group + list_agg
    df = df[list_col]
    group_df = df.groupby(list_group).agg(model)
    print(group_df)
    for item in group_df.index:
        print(item)
        print(group_df[item])
if __name__ == "__main__":
    pd_account(['lsje'],['y','lsfx'],'sum')

print(df)

        y lsfx      lsje
0    2023    +  14334.74
1    2023    -    107.00
..    ...  ...       ...
169  2023    -    461.00
170  2023    +    461.00
171  2023    -     15.80

[172 rows x 3 columns]

print(group_df

               lsje
y    lsfx          
2023 +     561520.79
     -     209005.97

遍历index(print(item))结果

===============================
(2023, '+')
(2023, '-')

通过Index获取值报错

C:\Users\MA110\AppData\Local\Programs\Python\Python311\python.exe E:\OneDrive\MyProject\pythonProject_AppAccount\FunAccount.py 
Traceback (most recent call last):
  File "C:\Users\MA110\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3652, in get_loc
    return self._engine.get_loc(casted_key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pandas\_libs\index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: (2023, '+')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "E:\OneDrive\MyProject\pythonProject_AppAccount\FunAccount.py", line 14, in <module>
    pd_account(['lsje'],['y','lsfx'],'sum')
  File "E:\OneDrive\MyProject\pythonProject_AppAccount\FunAccount.py", line 12, in pd_account
    print(group_df[item])
          ~~~~~~~~^^^^^^
  File "C:\Users\MA110\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 3761, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MA110\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3654, in get_loc
    raise KeyError(key) from err
KeyError: (2023, '+')

进程已结束,退出代码1
  • 写回答

3条回答 默认 最新

  • 大师兄6668 Python领域新星创作者 2023-06-15 17:35
    关注

    你那个print里面的group_df[item]出不来结果,因为item是数字不是列名

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月23日
  • 已采纳回答 6月15日
  • 创建了问题 6月14日

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了