2017-06-19 71 views
1

我有兩個熊貓系列(pandas.core.series.Series),我想合併成一個單一系列,增加對公共密鑰的值,例如:如何合併熊貓系列,並添加共同的價值觀

series1: 
AAA Championship Car season          1 
Act of Parliament of Ontario          1 
Act of Parliament of the United Kingdom       18 
Act of Parliament of the United Kingdom election law    1 

+

series2: 
ATP Buenos Aires             1 
ATP World Tour Finals            1 
Act of Parliament of British Columbia        1 
Act of Parliament of the United Kingdom       18 
Act of Parliament of the United Kingdom election law    1 

=

series3: 
AAA Championship Car season          1 
ATP Buenos Aires             1 
ATP World Tour Finals            1 
Act of Parliament of British Columbia        1 
Act of Parliament of Ontario          1 
Act of Parliament of the United Kingdom       36 
Act of Parliament of the United Kingdom election law    2 

回答

1

讓我們嘗試用pd.concatsum

series3 = pd.concat([series1,series2]).sum(level=0) 

輸出:

0 
AAA Championship Car season        1 
ATP Buenos Aires           1 
ATP World Tour Finals         1 
Act of Parliament of British Columbia     1 
Act of Parliament of Ontario        1 
Act of Parliament of the United Kingdom     36 
Act of Parliament of the United Kingdom election law  2 
Name: 1, dtype: int64 
2
full_df = (
    pd.concat([series1, series2], axis = 0) 
     .groupby(level=0) 
     .sum() 
) 
+0

感謝編輯@ScottBoston。 –