Python數(shù)據(jù)分析之pandas函數(shù)詳解
一、apply和applymap
1. 可直接使用NumPy的函數(shù)
示例代碼:
# Numpy ufunc 函數(shù) df = pd.DataFrame(np.random.randn(5,4) - 1) print(df) print(np.abs(df))
運行結(jié)果:
0 1 2 3
0 -0.062413 0.844813 -1.853721 -1.980717
1 -0.539628 -1.975173 -0.856597 -2.612406
2 -1.277081 -1.088457 -0.152189 0.530325
3 -1.356578 -1.996441 0.368822 -2.211478
4 -0.562777 0.518648 -2.007223 0.059411
0 1 2 3
0 0.062413 0.844813 1.853721 1.980717
1 0.539628 1.975173 0.856597 2.612406
2 1.277081 1.088457 0.152189 0.530325
3 1.356578 1.996441 0.368822 2.211478
4 0.562777 0.518648 2.007223 0.059411
2. 通過apply將函數(shù)應(yīng)用到列或行上
示例代碼:
# 使用apply應(yīng)用行或列數(shù)據(jù) #f = lambda x : x.max() print(df.apply(lambda x : x.max()))
運行結(jié)果:
0 -0.062413
1 0.844813
2 0.368822
3 0.530325
dtype: float64
3.注意指定軸的方向,默認axis=0,方向是列
示例代碼:
# 指定軸方向,axis=1,方向是行 print(df.apply(lambda x : x.max(), axis=1))
運行結(jié)果:
0 0.844813
1 -0.539628
2 0.530325
3 0.368822
4 0.518648
dtype: float64
4. 通過applymap將函數(shù)應(yīng)用到每個數(shù)據(jù)上
示例代碼:
# 使用applymap應(yīng)用到每個數(shù)據(jù) f2 = lambda x : '%.2f' % x print(df.applymap(f2))
運行結(jié)果:
0 1 2 3
0 -0.06 0.84 -1.85 -1.98
1 -0.54 -1.98 -0.86 -2.61
2 -1.28 -1.09 -0.15 0.53
3 -1.36 -2.00 0.37 -2.21
4 -0.56 0.52 -2.01 0.06
二、排序
1. 索引排序
sort_index()
排序默認使用升序排序,ascending=False 為降序排序
示例代碼:
# Series s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5)) print(s4) # 索引排序 s4.sort_index() # 0 0 1 3 3
運行結(jié)果:
0 10
3 11
1 12
3 13
0 14
dtype: int64
0 10
0 14
1 12
3 11
3 13
dtype: int64
2.對DataFrame操作時注意軸方向
示例代碼:
# DataFrame df4 = pd.DataFrame(np.random.randn(3, 5), index=np.random.randint(3, size=3), columns=np.random.randint(5, size=5)) print(df4) df4_isort = df4.sort_index(axis=1, ascending=False) print(df4_isort) # 4 2 1 1 0
運行結(jié)果:
1 4 0 1 2
2 -0.416686 -0.161256 0.088802 -0.004294 1.164138
1 -0.671914 0.531256 0.303222 -0.509493 -0.342573
1 1.988321 -0.466987 2.787891 -1.105912 0.889082
4 2 1 1 0
2 -0.161256 1.164138 -0.416686 -0.004294 0.088802
1 0.531256 -0.342573 -0.671914 -0.509493 0.303222
1 -0.466987 0.889082 1.988321 -1.105912 2.787891
3. 按值排序
sort_values(by='column name')
根據(jù)某個唯一的列名進行排序,如果有其他相同列名則報錯。
示例代碼:
# 按值排序 df4_vsort = df4.sort_values(by=0, ascending=False) print(df4_vsort)
運行結(jié)果:
1 4 0 1 2
1 1.988321 -0.466987 2.787891 -1.105912 0.889082
1 -0.671914 0.531256 0.303222 -0.509493 -0.342573
2 -0.416686 -0.161256 0.088802 -0.004294 1.164138
三、處理缺失數(shù)據(jù)
示例代碼:
df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan], [np.nan, 4., np.nan], [1., 2., 3.]]) print(df_data.head())
運行結(jié)果:
0 1 2
0 -0.281885 -0.786572 0.487126
1 1.000000 2.000000 NaN
2 NaN 4.000000 NaN
3 1.000000 2.000000 3.000000
1. 判斷是否存在缺失值:isnull()
示例代碼:
# isnull print(df_data.isnull())
運行結(jié)果:
0 1 2
0 False False False
1 False False True
2 True False True
3 False False False
2. 丟棄缺失數(shù)據(jù):dropna()
根據(jù)axis軸方向,丟棄包含NaN的行或列。 示例代碼:
# dropna print(df_data.dropna()) print(df_data.dropna(axis=1))
運行結(jié)果:
0 1 2
0 -0.281885 -0.786572 0.487126
3 1.000000 2.000000 3.000000
1
0 -0.786572
1 2.000000
2 4.000000
3 2.000000
3. 填充缺失數(shù)據(jù):fillna()
示例代碼:
# fillna print(df_data.fillna(-100.))
運行結(jié)果:
0 1 2
0 -0.281885 -0.786572 0.487126
1 1.000000 2.000000 -100.000000
2 -100.000000 4.000000 -100.000000
3 1.000000 2.000000 3.000000
到此這篇關(guān)于Python數(shù)據(jù)分析之pandas函數(shù)詳解的文章就介紹到這了,更多相關(guān)python的pandas函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python教程pandas數(shù)據(jù)分析去重復(fù)值
- Pandas數(shù)據(jù)分析之批量拆分/合并Excel
- python數(shù)據(jù)分析必會的Pandas技巧匯總
- Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)
- Python數(shù)據(jù)分析之pandas比較操作
- Pandas數(shù)據(jù)分析的一些常用小技巧
- Python Pandas數(shù)據(jù)分析工具用法實例
- 基于Python數(shù)據(jù)分析之pandas統(tǒng)計分析
- Python數(shù)據(jù)分析pandas模塊用法實例詳解
- 五個Pandas?實戰(zhàn)案例帶你分析操作數(shù)據(jù)
相關(guān)文章
Python 之pandas庫的安裝及庫安裝方法小結(jié)
Pandas 是一種開源的、易于使用的數(shù)據(jù)結(jié)構(gòu)和Python編程語言的數(shù)據(jù)分析工具,它與 Scikit-learn 兩個模塊幾乎提供了數(shù)據(jù)科學(xué)家所需的全部工具,今天通過本文給大家介紹Python 之pandas庫的安裝及庫安裝方法小結(jié),感興趣的朋友跟隨小編一起看看吧2022-11-11Python設(shè)計模式結(jié)構(gòu)型組合模式
這篇文章主要介紹了Python設(shè)計模式結(jié)構(gòu)型組合模式,組合模式即Composite?Pattern,將對象組合成成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),組合模式使得用戶對單個對象和組合對象的使用具有一致性,下文具有一定的參考價值,需要的小伙伴可以參考一下2022-02-02Python?識別錄音并轉(zhuǎn)為文字的實現(xiàn)
本文主要介紹了Python?識別錄音并轉(zhuǎn)為文字的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03windows下Anaconda的安裝與配置正解(Anaconda入門教程)
最近很多朋友學(xué)習(xí)python,很多朋友也推薦使用anaconda這個工具,但安裝以后也不會使用,這里腳本之家小編就為大家整理一下比較詳細的教程,方便自己也方便需要的朋友,希望大家以后多多支持腳本之家2018-04-04python深度學(xué)習(xí)tensorflow實例數(shù)據(jù)下載與讀取
這篇文章主要為大家介紹了python深度學(xué)習(xí)tensorflow實例數(shù)據(jù)下載與讀取示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Python基礎(chǔ)之教你怎么在M1系統(tǒng)上使用pandas
這篇文章主要介紹了Python基礎(chǔ)之教你怎么在M1系統(tǒng)上使用pandas,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05python pandas cumsum求累計次數(shù)的用法
這篇文章主要介紹了python pandas cumsum求累計次數(shù)的用法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07