Python 數(shù)據(jù)分析:Pandas 數(shù)據(jù)操作(二)
接上一節(jié):《Python 數(shù)據(jù)分析:Pandas 數(shù)據(jù)操作(一)》

4. 插入、新增列
繼續(xù)上述的案例數(shù)據(jù),我們演示在某個位置插入列,或者新增列的方法。
# 首先,在columns軸1處,插入f列,并將值設(shè)置為0-5.
>>>df.insert(1,'f',range(5))
>>>df
af b
00.498522 0 0
10.056069 1 0
20.728903 2 1
30.320384 3 0
40.175444 4 0
# 新增g列,并賦值為10
>>>df['g']=10
>>>df
af b g
00.498522 0 0 10
10.056069 1 0 10
20.728903 2 1 10
30.320384 3 0 10
40.175444 4 0 10可以看到.insert()方法會將新插入的列放到指定的位置,而變量名加[]的方法會將新插入的列放到最后的位置(另外,這里的方括號接受列表,也就是可以同時插入多列數(shù)據(jù))。在日常使用中,建議使用后一種方法,因為它效率較高。
5. 刪除
既然有插入列,就有刪除列的方法,這里我們繼續(xù)上面的案例數(shù)據(jù)做演示。
# 使用del刪除列
>>> deldf['f']
>>> df
abg
0 0.498522 0 10
1 0.056069 0 10
2 0.728903 1 10
3 0.320384 0 10
4 0.175444 0 10
# 使用drop()方法刪除1列
>>> df.drop('g',axis='columns',inplace=True)
ab
0 0.498522 0
1 0.056069 0
2 0.728903 1
3 0.320384 0
4 0.175444 0
# 使用drop()方法刪除多列
>>> df[['c','d']]=10
>>> df.drop(['c','d'],axis='columns',inplace=True)
>>> df
ab
0 0.498522 0
1 0.056069 0
2 0.728903 1
3 0.320384 0
4 0.175444 0使用del刪除列只能1列1列的刪除,而使用drop()方法可以一次性刪除多個列。但是要注意drop()方法中inplace默認(rèn)為False,意思是不修改原數(shù)據(jù),而是生成一個新的刪減后pandas數(shù)據(jù),必須將其設(shè)置為True才會在原數(shù)據(jù)上操作。
6. 重復(fù)值處理
重復(fù)值是我們遇到的最常見的數(shù)據(jù)處理問題之一,一般是需要清除的數(shù)據(jù),這里給出刪除重復(fù)數(shù)據(jù)的方法。
# 數(shù)據(jù)預(yù)處理
>>>df['a']=df['b']
# 刪除重復(fù)數(shù)據(jù)
>>>df.drop_duplicates(inplace=True)
ab
00 0
21 1
# 重新生成重復(fù)數(shù)據(jù)
>>>df1 = pd.concat([df,df,df]).reset_index()
indexa b
00 0 0
12 1 1
20 0 0
32 1 1
40 0 0
52 1 1
# 刪除a,b列重復(fù)值,并保留最后一個數(shù)
>>>df1.drop_duplicates(subset=['a','b'],keep='last')
indexa b
40 0 0
52 1 1
# 刪除a,b列重復(fù)值,并保留第一個數(shù)
>>>df1.drop_duplicates(subset=['a','b'],keep='first')
indexa b
00 0 0
12 1 1使用drop_duplicates()方法刪除重復(fù)值時,inplace=True表明直接在原數(shù)據(jù)上操作。上述對df1變量處理是,inplace是默認(rèn)值False,所以沒有修改df1,僅僅是返回了刪除重復(fù)值后的新數(shù)據(jù)(因為沒有新變量賦值,所以輸出到標(biāo)準(zhǔn)輸入輸出設(shè)備,即屏幕打印)。
7. 空處理
空值數(shù)據(jù),也是我們最常遇到的問題,通常我們會將存在空值的數(shù)據(jù)行刪除,但有時數(shù)據(jù)是有時間順序的,或許用前后數(shù)據(jù)來填充,效果更好。
# 生成演示數(shù)據(jù)
>>> df = pd.DataFrame(np.random.random(10).reshape(5,2),columns=['a','b'])
>>> df
a b
0 0.741999 0.202870
1 0.370957 0.115593
2 0.906677 0.836000
3 0.913933 0.882819
4 0.329363 0.677972
>>> df.iloc[1,0]=np.nan
>>> df.iloc[3,1]=np.nan
>>> df
a b
0 0.741999 0.202870
1 NaN 0.115593
2 0.906677 0.836000
3 0.913933 NaN
4 0.329363 0.677972一般直接刪除空值使用dropna()方法,同樣要注意它有inplace參數(shù),默認(rèn)為False。
# 直接刪除空值數(shù)據(jù)
>>> df.dropna()
a b
0 0.741999 0.202870
2 0.906677 0.836000
4 0.329363 0.677972如果不希望直接刪除空值行,也可以使用fillna()方法對空值進(jìn)行填充。它接受method關(guān)鍵字參數(shù),ffill表示前一個元素填充,bfill表示后一個元素填充。同樣要注意它有inplace參數(shù),默認(rèn)為False。
# 用前一個數(shù)據(jù)填充空值
>>> df.fillna(method='ffill')
a b
0 0.741999 0.202870
1 0.741999 0.115593
2 0.906677 0.836000
3 0.913933 0.836000
4 0.329363 0.677972
# 用后一個數(shù)據(jù)填充空值
>>> df.fillna(method='bfill')
a b
0 0.741999 0.202870
1 0.906677 0.115593
2 0.906677 0.836000
3 0.913933 0.677972
4 0.329363 0.677972也可以不適用method關(guān)鍵字參數(shù),直接輸入填充的值,如下:
>>> df.fillna(1)
ab
0 0.741999 0.202870
1 1.000000 0.115593
2 0.906677 0.836000
3 0.913933 1.000000
4 0.329363 0.677972這里.fillna()中的1為位置參數(shù),對應(yīng)value關(guān)鍵字,意思是用1填充所有空值。
小結(jié)
本節(jié)學(xué)習(xí)了Pandas數(shù)據(jù)的一般操作方法,需要掌握.loc[]、.iloc[]方法的使用,以及知道df[]是一種按列提取數(shù)據(jù)的方法。同時,需要掌握數(shù)據(jù)的一般操作、修改、新增和刪除方法,以及重復(fù)、空值的處理。


































