简体   繁体   中英

Pandas DataFrame manipulation with Date as index

I deal with DataFrame where indexes are dates:

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:

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

I want to fill in missing dates with 0s

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

If I understand correctly, You can use 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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