Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two series in pandas along their index [duplicate]

I have two series in pandas.

series 1:

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

and series 2:

id        count_2
1           3
3           1
4           1
5           2
6           1

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
like image 386
user7289 Avatar asked Aug 06 '13 14:08

user7289


1 Answers

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

Note: if these were DataFrame (rather than Series) you could use merge:

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
like image 148
Andy Hayden Avatar answered Oct 23 '22 03:10

Andy Hayden