中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python dataframe向下向上填充,fillna和ffill的方法

發布時間:2020-10-13 09:04:23 來源:腳本之家 閱讀:513 作者:chenKFKevin 欄目:開發技術

首先新建一個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的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

平利县| 磴口县| 银川市| 宝丰县| 西平县| 新河县| 城市| 武穴市| 香河县| 方城县| 城步| 翼城县| 永城市| 鹤峰县| 江油市| 酉阳| 东至县| 五莲县| 古丈县| 北碚区| 安康市| 巴青县| 那曲县| 资中县| 和平县| 灵寿县| 陇川县| 岱山县| 台中市| 天门市| 镇原县| 都昌县| 九寨沟县| 建瓯市| 广安市| 宁波市| 台湾省| 徐汇区| 普宁市| 宁强县| 顺平县|