gpt4 book ai didi

python - Pandas hub_table 与 aggfunc 在不同数据上的工作方式有所不同

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:34 24 4
gpt4 key购买 nike

我有一个包含日期买入卖出值的数据框,并在该数据框上执行pivot_table获取每个有效日期的所有买入/卖出值(value)。

data = [('20170325', 'Buy', 400 ),
('20170325', 'Buy', 401 ),
('20170323', 'Buy', 400 ),
('20170324', 'Sell', 400 )]

testDf = pd.DataFrame(data, columns=['Date', 'Scenario', 'Value'])
df1 = pd.pivot_table(testDf, columns='Scenario', index='Date', values='Value',
fill_value = '', aggfunc=lambda x: x.tolist() if len(x)>1 else x)
df1
# Scenario Buy Sell
# Date
# 20170323 400
# 20170324 400
# 20170325 [400, 401]

但是当我的数据发生一点变化时,pivot_table 会抛出错误:ValueError:函数不会减少。无法理解为什么。这是不同数据的错误。请注意,日期20170325已更改为20170321

data = [('20170321', 'Buy', 400 ),
('20170321', 'Buy', 401 ),
('20170323', 'Buy', 400 ),
('20170324', 'Sell', 400 )]

testDf = pd.DataFrame(data, columns=['Date', 'Scenario', 'Value'])
df1 = pd.pivot_table(testDf, columns='Scenario', index='Date', values='Value',
fill_value = '',aggfunc=lambda x: x.tolist() if len(x)>1 else x)

Traceback (most recent call last): File "", line 1, in
File "ext2\vc12_win32\lib\python2.7\site-packages\pandas\tools\pivot.py", line 114, in pivot_table
File "ext2\vc12_win32\lib\python2.7\site-packages\pandas\core\groupby.py", line 729, in agg
File "ext2\vc12_win32\lib\python2.7\site-packages\pandas\core\groupby.py", line 2978, in aggregate
File "ext2\vc12_win32\lib\python2.7\site-packages\pandas\core\groupby.py", line 1227, in _python_agg_general
File "ext2\vc12_win32\lib\python2.7\site-packages\pandas\core\groupby.py", line 1733, in agg_series
File "ext2\vc12_win32\lib\python2.7\site-packages\pandas\core\groupby.py", line 1767, in _aggregate_series_pure_python
ValueError: Function does not reduce

最佳答案

甚至考虑使用 pivotgroupby:

df1 = testDf.groupby(['Date', 'Scenario'])['Value'].apply(lambda x: list(x)).reset_index()\
.pivot(index='Date', columns='Scenario', values='Value').fillna('')

# Scenario Buy Sell
# Date
# 20170323 [400]
# 20170324 [400]
# 20170325 [400, 401]

# Scenario Buy Sell
# Date
# 20170321 [400, 401]
# 20170323 [400]
# 20170324 [400]

关于python - Pandas hub_table 与 aggfunc 在不同数据上的工作方式有所不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43497792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com