【发布时间】:2021-09-30 12:59:43
【问题描述】:
我在 groupby 之后遇到问题并收到此错误消息:
Traceback(最近一次调用最后一次): 文件“C:\Users\User\PycharmProjects\HashTag_Curso\venv\lib\site-packages\pandas\core\indexes\base.py”,第 3080 行,在 get_loc 返回 self._engine.get_loc(casted_key) 文件“pandas_libs\index.pyx”,第 70 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas_libs\index.pyx”,第 101 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas_libs\hashtable_class_helper.pxi”,第 4554 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item 文件“pandas_libs\hashtable_class_helper.pxi”,第 4562 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item KeyError:'Ano'
上述异常是以下异常的直接原因:
Traceback(最近一次调用最后一次): 文件“C:/Users/User/PycharmProjects/Bibliotecas/Exemplo.py”,第 11 行,在 x = dfg['Ano'] getitem 中的文件“C:\Users\User\PycharmProjects\HashTag_Curso\venv\lib\site-packages\pandas\core\frame.py”,第 3024 行 索引器 = self.columns.get_loc(key) 文件“C:\Users\User\PycharmProjects\HashTag_Curso\venv\lib\site-packages\pandas\core\indexes\base.py”,第 3082 行,在 get_loc 从错误中引发 KeyError(key) KeyError:'Ano'
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from astropy.stats import biweight_midcorrelation as bw_cor
df = pd.read_csv(r'Bases_dados\D_1_4M\Tudo/combined.csv').iloc[:100000]
df['Ano'] = df['Data decimal']//1
dfg = df.groupby(by=["Ano"]).mean()
print(dfg)
x = dfg['Ano']
y = dfg['Lances']
r = np.corrcoef(x, y)[0][1]
bwr = bw_cor(x, y)
print(bwr, r)
plt.scatter(x, y)
plt.show()
如果我使用 x = df['Ano'] y = df['Lances']
工作正常,但使用 dfg(按“Ano”分组),我会收到错误消息。
当我打印(dfg)时,“Ano”列正常显示。
【问题讨论】:
标签: pandas pandas-groupby