如何合并pandas系列并添加常用值

时间:2017-06-19 15:19:17

标签: python pandas merge

我有两个Pandas系列(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

3 个答案:

答案 0 :(得分:2)

full_df = (
    pd.concat([series1, series2], axis = 0)
      .groupby(level=0)
      .sum()
)

答案 1 :(得分:1)

让我们pd.concat尝试sum

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 :(得分:0)

您可以使用pandas.Series.sum() https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.add.html 这也提供了填补缺失值的方法。

例如,您的代码可能是

series3 = series1.add(series2, fill_value=0)
相关问题