我日期,購買和銷售值的數據幀,並在該數據幀做pivot_table
獲得其中工程每個日期的所有買入/賣出價值。熊貓pivot_table工作不同的一點不同的數據
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異常:功能不減少。無法理解爲什麼。這是不同數據的錯誤。請注意,日期的更改爲。
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
作爲@ScottBoston以下提到,在兩個數據集中出現的錯誤。 – Parfait