I have two DataFrames with the same number of rows - df1
like so:
date hour var1
a 2017-05-01 00:00:00 456585
b 2017-05-01 01:00:00 899875
c 2017-05-01 02:00:00 569566
d 2017-05-01 03:00:00 458756
e 2017-05-01 04:00:00 231458
f 2017-05-01 05:00:00 986545
and df2
like so:
MyVar1 MyVar2
0 6169.719338 3688.045368
1 5861.148007 3152.238704
2 5797.053347 2700.469871
3 5779.102340 2730.471948
4 6708.219647 3181.298291
5 8550.380343 3793.580394
I want to merge the data from the date
and hour
columns of df1
into df2
, to get a result like:
MyVar1 MyVar2 date hour
0 6169.719338 3688.045368 2017-05-01 00:00:00
1 5861.148007 3152.238704 2017-05-01 01:00:00
2 5797.053347 2700.469871 2017-05-01 02:00:00
3 5779.102340 2730.471948 2017-05-01 03:00:00
4 6708.219647 3181.298291 2017-05-01 04:00:00
5 8550.380343 3793.580394 2017-05-01 05:00:00
I tried simply assigning the columns like so:
df2['date'] = df1['date']
df2['hour'] = df1['hour']
but I get a result with NaN values in the date
and hour
columns instead:
MyVar1 MyVar2 date hour
0 6169.719338 3688.045368 NaN NaN
1 5861.148007 3152.238704 NaN NaN
2 5797.053347 2700.469871 NaN NaN
Why does this happen? How can I simply assign the values such that the data from the first row of df1
is shown in the first row of df2
, etc.?
ignore_index=True
? Otherwise, reset both indices first and then concatenate. – Corunna