简体   繁体   English

使用pandas将索引列添加到DataFrame

[英]Add indexed column to DataFrame with pandas

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. 我有一个带有pandas.DateRange索引的DataFrame,我想添加一个包含相同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

df['C'] = df2['C']也适用于这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM