简体   繁体   English

以Date为索引的Pandas DataFrame操作

[英]Pandas DataFrame manipulation with Date as index

I deal with DataFrame where indexes are dates: 我处理索引是日期的DataFrame:

Date        Status 1   
2015-01-01  1    
2015-02-01  2    
2015-03-01  2    
2015-04-01  3    
2015-05-01  5

I want to compare this DataFrame with a second one that is missing some rows: 我想将此DataFrame与缺少某些行的第二个DataFrame进行比较:

Date        Status 2    
2015-02-01  2    
2015-03-01  3    
2015-04-01  2    
2015-05-01  6

You can see that on 2015-01-01, I have no data 您可以看到2015年1月1日,我没有数据

I want to fill in missing dates with 0s 我想用0填写缺失的日期

Can anyone help me with an easy way? 谁能帮我一个简单的方法?

Thanks 谢谢

PS: Sorry for this post formatting... but I couldn't render nice rows and columns render like on my Spyder PS:很抱歉,这篇文章的格式...但是我无法像在Spyder上那样渲染漂亮的行和列

If I understand correctly, You can use reindex : 如果我理解正确,则可以使用reindex

df2.reindex(df1.index,fill_value=0)

            Status 2
Date                
2015-01-01         0
2015-02-01         2
2015-03-01         3
2015-04-01         2
2015-05-01         6
df = df.merge(df1,how='left', on='Date') #you can use 'outer' in how, if empty Date is in both sets
df.fillna(0,inplace=True)

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

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