我在这里有一个数据框:https://www.dropbox.com/s/ja6kn0f55599xul/test.csv
因此,我想计算 df.bid 的中位数,但要根据 df.candle 值,即:
对于整个数据帧,df.candle为0的行的中位数df.bid,df.candle为1的中位数df.bid,df.candle为2的中位数df.bid,等等。我也想将其存储在单独的列中。我认为类似的方法会起作用:
df['median'] = df.bid.groupby('candle').mean()
但是那也不行:
df['median'] = df['bid'].groupby('candle').mean()
两者均导致错误:
Traceback (most recent call last):
File "C:/Users/irmsc/pp/fml/mo18.py", line 27, in <module>
df['median'] = df['bid'].groupby('candle').mean()
File "C:\Users\irmsc\pp\fml\venv\lib\site-packages\pandas\core\generic.py", line 7632, in groupby
observed=observed, **kwargs)
File "C:\Users\irmsc\pp\fml\venv\lib\site-packages\pandas\core\groupby\groupby.py", line 2110, in groupby
return klass(obj, by, **kwds)
File "C:\Users\irmsc\pp\fml\venv\lib\site-packages\pandas\core\groupby\groupby.py", line 360, in __init__
mutated=self.mutated)
File "C:\Users\irmsc\pp\fml\venv\lib\site-packages\pandas\core\groupby\grouper.py", line 578, in _get_grouper
raise KeyError(gpr)
KeyError: 'candle'