2017-11-30 7 views
1

皆さんは以前私の質問に非常に役立っていました。私は、英数字の値を持つインデックスをソートすることを検討していました。 今日は成功した、このスクリプトを実行したが、エラーを受信されています:バックピボットにデータ操作 - データフレーム集約関数を使用

/Library/Python/2.7/site-packages/pandas/core/groupby.py:4036: FutureWarning: using a dict with renaming is deprecated and will be removed in a future version 
    return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs) 
Traceback (most recent call last) 
aggfunc={'sum': np.sum}, fill_value=0) 
    File "/Library/Python/2.7/site-packages/pandas/core/reshape/pivot.py", line 136, in pivot_table 
    agged = grouped.agg(aggfunc) 
    File "/Library/Python/2.7/site-packages/pandas/core/groupby.py", line 4036, in aggregate 
    return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs) 

トレース:

df = df.pivot_table(index=['customer'], columns=['Duration'], 
                aggfunc={'sum': np.sum}, 
    fill_value=0) 

このエラーがした前に、私が適用されてきた唯一の変更SQL文で計算を実行するのではなく、データフレームの1つのデータ列に計算を導入します。

新しい計算:

df['Duration'] = df['Duration']/30 

古いグループ・バイと集計:

df = df.pivot_table(index=['customer'], columns=['Duration'], 
              aggfunc={'sum': np.sum}, fill_value=0) 
c = df.columns.levels[1] 
c = sorted(ns.natsorted(c), key=lambda x: not x.isdigit()) 
df = df.reindex_axis(pd.MultiIndex.from_product([df.columns.levels[0], c]), axis=1) 

新しいコードスニペット:

:新しいアプローチと

df = df.groupby(['customer', 'Duration']).agg({'sum': np.sum}) 
c = df.columns.get_level_values(1) 
c = sorted(ns.natsorted(c), key=lambda x: not x.isdigit()) 
df = df.reindex_axis(pd.MultiIndex.from_product([df.columns.levels[0], c]), axis=1) 

マルチインデックス・レベルを

MultiIndex(levels=[[u'Invoice A', u'Invoice B', u'Invoice C', u'Invoice B'], [u'0', u'1', u'10', u'11', u'2', u'2Y', u'3', u'3Y', u'4', u'4Y', u'5', u'5Y', u'6', u'7', u'8', u'9', u'9Y']], labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]], names=['customer', u'Duration']) 
IndexError: Too many levels: Index has only 1 level, not 2

入力サンプル:

このc = df.columns.get_level_values(1)を割り当て、私はエラーメッセージが表示され

customer    Duration    sum   
Invoice A    1     1250 
Invoice B    2     2000 
Invoice B    3     1200 
Invoice C    2     10250 
Invoice D    3     20500 
Invoice D    5     18900 
Invoice E    2Y    5000 
Invoice F    1     5000 
Invoice F    1Y    12100 

わからない理由を、両方のレベルと名前は、以下の2つのレベルを持っているよう。 最終結果は、customerでソートされたデータフレームで、Durationsumを示す列がDurationでソートされています。また、以前のコードバージョンでピボットを使用した理由は、この出力形式を維持するためです:

Duration       2   2Y   3   3Y 
customer                  
Invoice A       2550  0.00  0.00  2000 
Invoice B       5000  2500  1050  0.00 
Invoice C       12500  0.00  1120  2050 
Invoice D       0.00  1500  0.00  8010 

私は正しいトラックにいますか?

Data Manipulation - stackoverflow

+0

そのハードでは見つけること。おそらくあなたはこれを探しているでしょう。https://stackoverflow.com/questions/44635626/pandas-aggregation-warning-futurewarning-using-a-dict-with-renaming-is-depreca – Dark

+0

そして、あなたは列のレベルを探しています。make 'df.index.get_level_values'にする必要があります。 – Dark

答えて

1

あなたはinstaed agg機能sum()を使用することができ、その後、unstackによって再構築:実際の質問は、あなたの質問にある場合

import natsort as ns 

df = df.groupby(['customer', 'Duration'])['sum'].sum().unstack() 

c = sorted(ns.natsorted(df.columns), key=lambda x: not x.isdigit()) 
df = df.reindex(columns=c) 
print (df) 
Duration  1  2  3  5  1Y  2Y 
customer              
Invoice A 1250.0  NaN  NaN  NaN  NaN  NaN 
Invoice B  NaN 2000.0 1200.0  NaN  NaN  NaN 
Invoice C  NaN 10250.0  NaN  NaN  NaN  NaN 
Invoice D  NaN  NaN 20500.0 18900.0  NaN  NaN 
Invoice E  NaN  NaN  NaN  NaN  NaN 5000.0 
Invoice F 5000.0  NaN  NaN  NaN 12100.0  NaN 
+0

jezrael - あなたのソリューションはよさそうです。私は 'df.reindex(c、axis = 1)'で奇妙なエラーが発生しています。 "ファイル" /Library/Python/2.7/site-packages/pandas/core/generic.py "、2494行目、再インデクス '引数" {0} "'。(list(kwargs.keys())[0 ])) TypeError:reindex()は予期しないキーワード引数 "axis"を持っています – OAK

+1

多分、最後のバージョンのpandasで、 'df = df.reindex(columns = c)' – jezrael

+0

を試してみてください。どうもありがとう! – OAK

関連する問題