Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何合并熊猫系列并添加公共值_Python_Pandas_Merge - Fatal编程技术网

Python 如何合并熊猫系列并添加公共值

Python 如何合并熊猫系列并添加公共值,python,pandas,merge,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 Kingdo

我有两个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
+

=


您可以使用
pandas.Series.sum()
这也提供了一种填充缺失值的方法

例如,您的代码可以是

series3 = series1.add(series2, fill_value=0)

让我们用
sum
尝试
pd.concat

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
full_df = (
    pd.concat([series1, series2], axis = 0)
      .groupby(level=0)
      .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