首先新建一个dataframe:
In[8]: df = pd.DataFrame({'name':list('ABCDA'),'house':[1,1,2,3,3],'date':['2010-01-01','2010-06-09','2011-12-03','2011-04-05','2012-03-23']}) In[9]: df Out[9]: date house name 0 2010-01-01 1 A 1 2010-06-09 1 B 2 2011-12-03 2 C 3 2011-04-05 3 D 4 2012-03-23 3 A
将date列改为时间类型:
In[12]: df.date = pd.to_datetime(df.date)
数据的含义是这样的,我们有ABCD四个人的数据,已知A在2010-01-01的时候,名下有1套房,B在2010-06-09的时候,名下有1套房,C在2011-12-03的时候,有2套房,D在2011-04-05的时候有3套房,A在2012-02-23的时候,数据更新了,有两套房。
要求在有姓名和时间的情况下,能给出其名下有几套房:
比如A在2010-01-01与2012-03-23期间任意一天,都应该是1套房,在2012-03-23之后,都是3套房。
我们使用pandas的fillna方法,选择ffill。
首先我们获得一个2010-01-01到2017-12-01的dataframe
In[14]: time_range = pd.DataFrame( pd.date_range('2010-01-01','2017-12-01',freq='D'), columns=['date']).set_index("date") In[15]: time_range Out[15]: Empty DataFrame Columns: [] Index: [2010-01-01 00:00:00, 2010-01-02 00:00:00, 2010-01-03 00:00:00, 2010-01-04 00:00:00, 2010-01-05 00:00:00, 2010-01-06 00:00:00, 2010-01-07 00:00:00, 2010-01-08 00:00:00, 2010-01-09 00:00:00, 2010-01-10 00:00:00, 2010-01-11 00:00:00, 2010-01-12 00:00:00, 2010-01-13 00:00:00, 2010-01-14 00:00:00, 2010-01-15 00:00:00, 2010-01-16 00:00:00, 2010-01-17 00:00:00, 2010-01-18 00:00:00, 2010-01-19 00:00:00, 2010-01-20 00:00:00, 2010-01-21 00:00:00, 2010-01-22 00:00:00, 2010-01-23 00:00:00, 2010-01-24 00:00:00, 2010-01-25 00:00:00, 2010-01-26 00:00:00, 2010-01-27 00:00:00, 2010-01-28 00:00:00, 2010-01-29 00:00:00, 2010-01-30 00:00:00, 2010-01-31 00:00:00, 2010-02-01 00:00:00, 2010-02-02 00:00:00, 2010-02-03 00:00:00, 2010-02-04 00:00:00, 2010-02-05 00:00:00, 2010-02-06 00:00:00, 2010-02-07 00:00:00, 2010-02-08 00:00:00, 2010-02-09 00:00:00, 2010-02-10 00:00:00, 2010-02-11 00:00:00, 2010-02-12 00:00:00, 2010-02-13 00:00:00, 2010-02-14 00:00:00, 2010-02-15 00:00:00, 2010-02-16 00:00:00, 2010-02-17 00:00:00, 2010-02-18 00:00:00, 2010-02-19 00:00:00, 2010-02-20 00:00:00, 2010-02-21 00:00:00, 2010-02-22 00:00:00, 2010-02-23 00:00:00, 2010-02-24 00:00:00, 2010-02-25 00:00:00, 2010-02-26 00:00:00, 2010-02-27 00:00:00, 2010-02-28 00:00:00, 2010-03-01 00:00:00, 2010-03-02 00:00:00, 2010-03-03 00:00:00, 2010-03-04 00:00:00, 2010-03-05 00:00:00, 2010-03-06 00:00:00, 2010-03-07 00:00:00, 2010-03-08 00:00:00, 2010-03-09 00:00:00, 2010-03-10 00:00:00, 2010-03-11 00:00:00, 2010-03-12 00:00:00, 2010-03-13 00:00:00, 2010-03-14 00:00:00, 2010-03-15 00:00:00, 2010-03-16 00:00:00, 2010-03-17 00:00:00, 2010-03-18 00:00:00, 2010-03-19 00:00:00, 2010-03-20 00:00:00, 2010-03-21 00:00:00, 2010-03-22 00:00:00, 2010-03-23 00:00:00, 2010-03-24 00:00:00, 2010-03-25 00:00:00, 2010-03-26 00:00:00, 2010-03-27 00:00:00, 2010-03-28 00:00:00, 2010-03-29 00:00:00, 2010-03-30 00:00:00, 2010-03-31 00:00:00, 2010-04-01 00:00:00, 2010-04-02 00:00:00, 2010-04-03 00:00:00, 2010-04-04 00:00:00, 2010-04-05 00:00:00, 2010-04-06 00:00:00, 2010-04-07 00:00:00, 2010-04-08 00:00:00, 2010-04-09 00:00:00, 2010-04-10 00:00:00, ...] [2892 rows x 0 columns]
然后用上上篇博客中提到的pivot_table将原本的df转变之后,与time_range进行merger操作。
In[16]: df = pd.pivot_table(df, columns='name', index='date') In[17]: df Out[17]: house name A B C D date 2010-01-01 1.0 NaN NaN NaN 2010-06-09 NaN 1.0 NaN NaN 2011-04-05 NaN NaN NaN 3.0 2011-12-03 NaN NaN 2.0 NaN 2012-03-23 3.0 NaN NaN NaN In[18]: df = df.merge(time_range,how="right", left_index=True, right_index=True)
然后再进行向下填充操作:
In[20]: df = df.fillna(method='ffill')
最后:
df = df.stack().reset_index()
结果太长,这里就不粘贴了。如果想向上填充,可选择method = 'bfill‘
以上这篇python dataframe向下向上填充,fillna和ffill的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 魔兽世界wlk敏锐贼一键输出宏是什么 wlk敏锐贼一键输出宏介绍
- 李逸朗2007-李威乐[英皇娱乐][WAV+CUE]
- DavidVersace-EyetoEye(2024)[24-44,1]
- SimoneLamsma,RobertKulek-Mendelssohn,Janacek,Schumann-SonatasforViolinandPiano【SAC
- 《街头篮球》大动作:新增五大服务器节点 优化玩家体验
- 任天堂宣布推出音乐APP!仅面向NSO订阅用户
- 《马路RPG:兄弟齐航》新预告!兄弟携手海岛冒险
- 王子鸣.2009-永远【九洲】【WAV分轨】
- 杭天琪.2002-水与火的缠绵【泰达】【WAV+CUE】
- 邱锋泽.2024-COlOR.Free【发现梦想】【FLAC分轨】
- 群星 《10年朋友Vol.8》滚石台湾版[WAV+CUE][1.G]
- 群星《2024好听新歌31》十倍音质 U盘音乐 [WAV分轨][1.1G]
- 群星《全天后》华纳[WAV+CUE][1.1G]
- 许乐《声愿HQCD》[正版原抓WAV+CUE]
- 黄凯芹《给你留念》头版限量编号MQA-UHQCD2024[低速原抓WAV+CUE]