结合大 pandas 的两个系列及其索引 [英] Combining two series in pandas along their index

查看:57
本文介绍了结合大 pandas 的两个系列及其索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫中有两个系列.

I have two series in pandas.

系列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结合起来以形成下面的内容?

How do I combine the tables along the ids to form the below?

id        count_1    count_2
1            3        3
3           19        1
4           15        1
5            5        2
6            2        1

推荐答案

您可以使用

You can use concat:

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

注意:如果这些是DataFrame(而不是Series),则可以使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆