2017-10-12 6 views
1

これはthis answerと少し似ていますが、私の問題は少し異なります。パンダ:行の値を符号化する/データフレームを変換する

マイデータフレーム:

df=pd.DataFrame([{'date': '2017-01-01', 'id': 'google', 'category': 1, 'Score1': 51, 'Score2': 531}, 
     {'date': '2017-01-01', 'id': 'google', 'category': 2, 'Score1': 592, 'Score2': 152}, 
     {'date': '2017-01-01', 'id': 'google', 'category': 5, 'Score1': 55, 'Score2': 255}, 
     {'date': '2017-01-01', 'id': 'yahoo', 'category': 7, 'Score1': 597, 'Score2': 357}, 
     {'date': '2017-01-01', 'id': 'yahoo', 'category': 8, 'Score1': 58, 'Score2': 58}, 
     {'date': '2017-01-02', 'id': 'google', 'category': 5, 'Score1': 795, 'Score2': 455}, 
     {'date': '2017-01-02', 'id': 'google', 'category': 1, 'Score1': 71, 'Score2': 751}, 
     {'date': '2017-01-02', 'id': 'google', 'category': 2, 'Score1': 792, 'Score2': 352}, 
     {'date': '2017-01-02', 'id': 'yahoo', 'category': 7, 'Score1': 77, 'Score2': 957}, 
     {'date': '2017-01-02', 'id': 'yahoo', 'category': 8, 'Score1': 798, 'Score2': 358} 
     ]) 

そして、次のようになります。

  date  id category Score1 Score2 
0 2017-01-01 google   1  51  531 
1 2017-01-01 google   2  592  152 
2 2017-01-01 google   5  55  255 
3 2017-01-01 yahoo   7  597  357 
4 2017-01-01 yahoo   8  58  58 
5 2017-01-02 google   5  795  455 
6 2017-01-02 google   1  71  751 
7 2017-01-02 google   2  792  352 
8 2017-01-02 yahoo   7  77  957 
9 2017-01-02 yahoo   8  798  358 

私はこのようになりますデータフレームにこれを変換する必要があります。

date id cat1_score1 cat2_score1 cat5_score1 cat7_score1 cat8_score1 cat1_score2 cat2_score2 cat5_score2 cat7_score2 cat8_score2 
1/1/17 google 51  592   55   0   0   531  152  255   0   0 
1/1/17 yahoo  0  0   0   597   58    0   0   0   357  58 
1/2/17 google 71  792  795   0   0   751  352  455   0   0 
1/2/17 yahoo  0   0   0   77   798   0   0   0   957  358 

ここで注意すべき点がありますカテゴリの数はidからidまで変わる可能性があります。また、潜在的に考慮する必要がある二次ID列が存在する可能性があります。 id列の値をenumerateにすることはできますが、それに応じてデータフレームをどのように変換するのですか?

答えて

3

はのは次のように再構築してみましょう:

df_out = df.set_index(['date', 'id', 'category']).stack().unstack([2, 3]).fillna(0) 
df_out.columns = df_out.columns.map('{0[0]}_{0[1]}'.format) 
df_out = df_out.add_prefix('cat').reset_index() 
print(df_out) 

出力:

  date  id cat1_Score1 cat1_Score2 cat2_Score1 cat2_Score2 \ 
0 2017-01-01 google   51.0  531.0  592.0  152.0 
1 2017-01-01 yahoo   0.0   0.0   0.0   0.0 
2 2017-01-02 google   71.0  751.0  792.0  352.0 
3 2017-01-02 yahoo   0.0   0.0   0.0   0.0 

    cat5_Score1 cat5_Score2 cat7_Score1 cat7_Score2 cat8_Score1 \ 
0   55.0  255.0   0.0   0.0   0.0 
1   0.0   0.0  597.0  357.0   58.0 
2  795.0  455.0   0.0   0.0   0.0 
3   0.0   0.0   77.0  957.0  798.0 

    cat8_Score2 
0   0.0 
1   58.0 
2   0.0 
3  358.0 

説明:スコアリング列以外のすべてのまず​​、その後、垂直方向にすべての項目を見る揃えるためにstackを使用しています。次に、unstackを使用してカテゴリとスコアラベルを列にピボットし、fillnaをゼロにします。カラムmapを使用してカラムレベルを1つのレベルに結合し、add_prefix 'cat'をカラムに結合します。最後に、reset_indexはすべての列をインデックスからデータフレームに戻します。

+0

ありがとう@Scott - これはデータフレーム列から 'date'と' id'を削除したようです。 – Craig

+0

最後をデータフレームに戻すために、最後をリセットすることができます。 –

+0

@Craig更新しました.... –

関連する問題