Python 如何合并熊猫系列并添加公共值
我有两个Pandas系列(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.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