python數(shù)據(jù)分析apply(),map(),applymap()用法
在python的數(shù)據(jù)分析中,使用apply(),map(),applymap(),可以方便地實現(xiàn)對批量數(shù)據(jù)的自定義操作。其用法歸納如下。
| 函數(shù) | 用法 |
|---|---|
| apply() | 用于對DataFrame中的數(shù)據(jù)進行按行或者按列 操作 |
| map() | 用于對Series中的每一個數(shù)據(jù) 操作 |
| applymap() | 用于對DataFrame的 每一個數(shù)據(jù)操作 |
1.示例
apply()
apply()用于對DataFrame中的數(shù)據(jù)進行按行或者按列 操作。
import pandas as pd
data = [[110, 120, 110], [130, 130, 130], [130, 120, 130]]
columns = ['語文', '數(shù)學', '英語']
df = pd.DataFrame(data=data, columns=columns)
print(df)
print("=============================")
print(df.apply(lambda x: x.sum(), axis=1))
其中axis=1表示對行操作。若axis為0則表示對列操作。
map()
map()用于對Series中的每一個數(shù)據(jù) 操作。
import pandas as pd
s1 = pd.Series([11, 22, 33, 44, 55])
print(s1)
print("================================")
print(s1.map(lambda x: str(x)))applymap
applymap()用于對DataFrame的 每一個數(shù)據(jù)操作。
操作DataFrame的每一個數(shù)據(jù)。
以將每一個數(shù)據(jù)保留兩位小數(shù)為例:
import pandas as pd
data = [[110, 120, 110], [130, 130, 130], [130, 120, 130]]
columns = ['語文', '數(shù)學', '英語']
df = pd.DataFrame(data=data, columns=columns)
print(df)
print("=============================")
print(df.applymap(lambda x: '%.2f'%x))

到此這篇關于python數(shù)據(jù)分析apply(),map(),applymap()用法的文章就介紹到這了,更多相關apply(),map(),applymap()用法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python獲取CPU、內存使用率以及網絡使用狀態(tài)代碼
這篇文章主要介紹了Python獲取CPU使用率、內存使用率、網絡使用狀態(tài)的相關代碼,對此有需要的朋友一起測試下。2018-02-02

