Joining two rows in pandas along their index

I have two episodes 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 to combine tables by identifiers to form below?

 id count_1 count_2 1 3 3 3 19 1 4 15 1 5 5 2 6 2 1 
+6
source share
1 answer

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 it is a DataFrame (not a series), you can 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 
+13
source

Source: https://habr.com/ru/post/951141/


All Articles