2017-03-22 6 views
1

セル値を2つのデータフレームをマージしてまとめたもの。ここでパンダは私が<strong>月</strong>列でマージし、<strong>fullprice</strong>、<strong>actualprice</strong>と<strong>割引</strong>行を合計しようとしている2つのデータフレームがあり

は例です:

# First DataFrame 
    month fullprice actualprice discount 
0 Jan   10   7   3 
1 Feb   6   4   2 


# Second DataFrame 
    month fullprice actualprice discount 
0 Jan   11   5   6 
1 Feb   6   4   2 
2 Mar  100   50  50 


# Desired result 
    month fullprice actualprice discount 
0 Jan   21   12  9 
1 Feb   12    8  4 
2 Mar  100   50  50 

は、いくつかの方法を試してみましたが、それは私が必要なものではありません。

df1 = pd.DataFrame([['Jan', 10, 7, 3], ['Feb', 6, 4, 2]], columns=['month', 'fullprice', 'actualprice', 'discount']) 
df2 = pd.DataFrame([['Jan', 11, 5, 6], ['Feb', 6, 4, 2], ['Mar', 100, 50, 50]], columns=['month', 'fullprice', 'actualprice', 'discount']) 

df2.add(df1) 

     month fullprice actualprice discount 
    0 JanJan  21.0   12.0  9.0 
    1 FebFeb  12.0   8.0  4.0 
    2  NaN  NaN   NaN  NaN 

df1.merge(df2, how='right') 

     month fullprice actualprice discount 
    0 Feb   6   4   2 
    1 Jan   11   5   6 
    2 Mar  100   50  50 

df1.merge(df2, on='month', how='right') 

     month fullprice_x actualprice_x discount_x fullprice_y actualprice_y \ 
    0 Jan   10.0   7.0   3.0   11    5 
    1 Feb   6.0   4.0   2.0   6    4 
    2 Mar   NaN   NaN   NaN   100    50 

     discount_y 
    0   6 
    1   2 
    2   50 

任意のアイデアそれをマージする方法?

+0

Dataframe.appendその後、Dataframe.groupbyはトリックを行うべきではありません – alex314159

答えて

3

append then groupbyを使用します。

df1 = df1.set_index('month') 
df2 = df2.set_index('month') 
df1.append(df2).groupby(level=0).sum() 

     fullprice actualprice discount 
month         
Feb   12   8   4 
Jan   21   12   9 
Mar   100   50  50 

またはまったくインデックスの場合:

df1.append(df2).groupby('month').sum() 
関連する問題

 関連する問題