Python 将大熊猫的两个系列结合在一起
我有两个熊猫系列 系列1:Python 将大熊猫的两个系列结合在一起,python,pandas,series,Python,Pandas,Series,我有两个熊猫系列 系列1: id count_1 1 3 3 19 4 15 5 5 6 2 和系列2: id count_2 1 3 3 1 4 1 5 2 6 1 我如何沿ID组合表格以形成以下内容 id count_1 count_2
id count_1
1 3
3 19
4 15
5 5
6 2
和系列2:
id count_2
1 3
3 1
4 1
5 2
6 1
我如何沿ID组合表格以形成以下内容
id count_1 count_2
1 3 3
3 19 1
4 15 1
5 5 2
6 2 1
您可以使用:
注意:如果这些是数据帧(而不是系列),则可以使用:
感谢您的帮助-在第一个示例中,列1中的“id”位于“count_1”和“count_2”列标题下的一行中-要将它们全部放在同一行中,我是否只需遵循第二个示例?您可以使用reset_index,它将索引移动到其中一列。@user7289即
pd.concat([s1,s2],axis=1)。reset_index()
感谢Andy,注释中的最后一种方法有效…我似乎无法让合并工作,尽管…现在排序:)如何按索引合并两个索引不重叠的系列?
In [11]: s1
Out[11]:
id
1 3
3 19
4 15
5 5
6 2
Name: count_1, dtype: int64
In [12]: s2
Out[12]:
id
1 3
3 1
4 1
5 2
6 1
Name: count_2, dtype: int64
In [13]: pd.concat([s1, s2], axis=1)
Out[13]:
count_1 count_2
id
1 3 3
3 19 1
4 15 1
5 5 2
6 2 1
In [21]: df1 = s1.reset_index()
In [22]: s1.reset_index()
Out[22]:
id count_1
0 1 3
1 3 19
2 4 15
3 5 5
4 6 2
In [23]: df2 = s2.reset_index()
In [24]: df1.merge(df2)
Out[24]:
id count_1 count_2
0 1 3 3
1 3 19 1
2 4 15 1
3 5 5 2
4 6 2 1