python - Copying a column from one DataFrame to another gives NaN values? -


this question has been asked many times, , seemed work others, however, getting nan values when copy column different dataframe(df1 , df2 same length).

df1

        date     hour      var1 0   2017-05-01  00:00:00   456585 1   2017-05-01  01:00:00   899875 2   2017-05-01  02:00:00   569566 3   2017-05-01  03:00:00   458756 4   2017-05-01  04:00:00   231458 5   2017-05-01  05:00:00   986545 

df2

      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 need in df2

       myvar1    myvar2        date        time  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 following,

df2['date'] = df1['date'] df2['hour'] = df1['hour']  type(df1) >> pandas.core.frame.dataframe  type(df2) >> pandas.core.frame.dataframe 

i getting following,

       myvar1    myvar2      date       time  0  6169.719338 3688.045368  nan        nan  1  5861.148007 3152.238704  nan        nan  2  5797.053347 2700.469871  nan        nan 

why happening? there post discusses merge, need copy it. appreciated.

i believe dataframe index not same. reset index first.

df1 = df1.reset_index(drop=true) df2 = df2.reset_index(drop=true) 

now, can assign:

df2['date'] = df1['date'] df2['hour'] = df1['hour'] 

you can use pd.concat:

df = pd.concat([df2, df1[['date', 'hour']]], axis=1) print(df)           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 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -