Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two dataframes with different indexes

Tags:

python

pandas

I have two dataframes:

df1 (index is date):

             a  b
1900-01-01   1  2
1900-01-02   1  3
1900-01-03   3  3

df2 (index is int):

    c
0   3 
1   1

after merge:

             a  b  d
1900-01-01   1  2  3
1900-01-02   1  3  1
1900-01-03   3  3

Which function should I use?

like image 396
Panda Avatar asked Jul 17 '26 12:07

Panda


1 Answers

Adding values and using at

df1.at[:len(df2),'d']=df2.c.values
df1
Out[1200]: 
            a  b    d
1900-01-01  1  2  3.0
1900-01-02  1  3  1.0
1900-01-03  3  3  NaN
like image 109
BENY Avatar answered Jul 20 '26 02:07

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!