14
votes

I'm a beginning pandas user, and after studying the documentation I still can't find a straightforward way to do the following.

I have a DataFrame with a pandas.DateRange index, and I want to add a column with values for part of the same DateRange.

Suppose I have

df

                            A         B
2010-01-01 00:00:00  0.340717  0.702432
2010-01-01 01:00:00  0.649970  0.411799
2010-01-01 02:00:00  0.932367  0.108047
2010-01-01 03:00:00  0.051942  0.526318
2010-01-01 04:00:00  0.518301  0.057809
2010-01-01 05:00:00  0.779988  0.756221
2010-01-01 06:00:00  0.597444  0.312495

and

df2

                     C
2010-01-01 03:00:00  5
2010-01-01 04:00:00  5
2010-01-01 05:00:00  5

How can I obtain something like this:

                            A         B    C
2010-01-01 00:00:00  0.340717  0.702432    nan
2010-01-01 01:00:00  0.649970  0.411799    nan
2010-01-01 02:00:00  0.932367  0.108047    nan
2010-01-01 03:00:00  0.051942  0.526318    5
2010-01-01 04:00:00  0.518301  0.057809    5
2010-01-01 05:00:00  0.779988  0.756221    5
2010-01-01 06:00:00  0.597444  0.312495    nan
2

2 Answers

4
votes

df['C'] = df2['C'] should also work in this case.