我是熊貓新手,正在測試和學習。有從Excel導入的數據框下面的問題: - 數據框包含以下變量:熊貓Pivot_table執行時出錯 - 類型錯誤
<class 'pandas.core.frame.DataFrame'>
Int64Index: 48062 entries, 0 to 48061
Data columns (total 11 columns):
Konskund_MEAB 48062 non-null values
Strukturordn 48062 non-null values
Antal_forsandelser 48062 non-null values
ProdID 48062 non-null values
Sort 48062 non-null values
Storstad 48062 non-null values
Year 48062 non-null values
snittvikt 48062 non-null values
Totsum 48062 non-null values
Prodsum 48062 non-null values
snittpris 48062 non-null values
dtypes: float64(9), object(2)
運行:
np.average(df['snittpris'],weights=df['Antal_forsandelser'])
產生正確的結果
當我嘗試使用下面的命令運行pivot_table:
df_sum=pd.pivot_table(df,rows=['Konskund_MEAB','ProdID'],cols=['Year'], aggfunc=np.average(df ['snittpris'],weights=df['Antal_forsandelser']))
我收到以下錯誤消息。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-90-9fd03896c806> in <module>()
----> 1 df_sum=pd.pivot_table(df,rows=['Konskund_MEAB','ProdID'],cols=['Year'],
aggfunc=np.average(df['snittpris'],weights=df['Antal_forsandelser']))
C:\Users\Bengtw\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\tools\pivot.pyc
in pivot_table(data, values, rows, cols, aggfunc, fill_value, margins, dropna)
101
102 grouped = data.groupby(keys)
--> 103 agged = grouped.agg(aggfunc)
104
105 table = agged
C:\Users\Bengtw\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\groupby.pyc
in agg(self, func, *args, **kwargs)
342 @Appender(_agg_doc)
343 def agg(self, func, *args, **kwargs):
--> 344 return self.aggregate(func, *args, **kwargs)
345
346 def _iterate_slices(self):
C:\Users\Bengtw\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\groupby.pyc
in aggregate(self, arg, *args, **kwargs)
1741
1742 if self.grouper.nkeys > 1:
-> 1743 return self._python_agg_general(arg, *args, **kwargs)
1744 else:
1745 result = self._aggregate_generic(arg, *args, **kwargs)
C:\Users\Bengtw\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\groupby.pyc
in _python_agg_general(self, func, *args, **kwargs)
480
481 if len(output) == 0:
--> 482 return self._python_apply_general(f)
483
484 if self.grouper._filter_empty_groups:
C:\Users\Bengtw\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\groupby.pyc
in _python_apply_general(self, f)
332
333 def _python_apply_general(self, f):
--> 334 keys, values, mutated = self.grouper.apply(f, self.obj, self.axis)
335
336 return self._wrap_applied_output(keys, values,
C:\Users\Bengtw\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\groupby.pyc
in apply(self, f, data, axis, keep_internal)
628 # group might be modified
629 group_axes = _get_axes(group)
--> 630 res = f(group)
631 if not _is_indexed_like(res, group_axes):
632 mutated = True
C:\Users\Bengtw\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\groupby.pyc
in <lambda>(x)
468 def _python_agg_general(self, func, *args, **kwargs):
469 func = _intercept_function(func)
--> 470 f = lambda x: func(x, *args, **kwargs)
471
472 # iterate through "columns" ex exclusions to populate output dict
TypeError: 'numpy.float64' object is not callable
什麼問題?行變量Konskund_MEAB包含字符串(幾百個不同),ProdID是數字並具有4個唯一值。年份就是這樣(4個離散值)。
我認爲這是'aggfunc',按你的對方回答關於**彩* * :)。 –
嘗試了你的建議,這似乎是我想要的。獲取一組以「KeyError:'snittpris'」結尾的錯誤消息「 – user1160760
@ user1160760 hmmm此功能似乎無法訪問該列,似乎也得到了涉及SNDArray的內容......非常奇怪。 :( –