pandas之filter
數(shù)據(jù)準(zhǔn)備
import pandas as pd
product_info = {
"訂單號(hào)": [ "2951110000099262111", "2181910000909928191", "2194560000121355545", "1194560000121311126", "1483160000121315483"],
"數(shù)量": [92, 61, 66, 33, 15],
"價(jià)格(USD)": [230, 122, 150, 190, 200],
"狀態(tài)": ["Not Delivered", "Not Delivered", "Not Delivered", "Not Shipped", "Not Delivered"],
"訂單日期": ["2022-02-12", "2022-03-02", "2022-01-22", "2022-02-22", "2022-02-15"],
"訂單編號(hào)": ["444111", "444122", "444132", "444003", "444244"],
}
df = pd.DataFrame(product_info, index=['one', 'two', 'three', 'four', 'five'])
print(df)
'''
訂單號(hào) 數(shù)量 價(jià)格(USD) 狀態(tài) 訂單日期 訂單編號(hào)
one 2951110000099262111 92 230 Not Delivered 2022-02-12 444111
two 2181910000909928191 61 122 Not Delivered 2022-03-02 444122
three 2194560000121355545 66 150 Not Delivered 2022-01-22 444132
four 1194560000121311126 33 190 Not Shipped 2022-02-22 444003
five 1483160000121315483 15 200 Not Delivered 2022-02-15 444244
'''
方法介紹
按columns過濾
全列名過濾
# 過濾出列名為“訂單號(hào)”和“訂單編號(hào)”列
res_df = df.filter(items=["訂單號(hào)", '訂單編號(hào)'])
print(res_df)
'''
訂單號(hào) 訂單編號(hào)
one 2951110000099262111 444111
two 2181910000909928191 444122
three 2194560000121355545 444132
four 1194560000121311126 444003
five 1483160000121315483 444244
'''
模糊過濾
# 過濾出列名中包含“訂單”的所有列
res_df = df.filter(like='訂單', axis=1)
print(res_df)
'''
訂單號(hào) 訂單日期 訂單編號(hào)
one 2951110000099262111 2022-02-12 444111
two 2181910000909928191 2022-03-02 444122
three 2194560000121355545 2022-01-22 444132
four 1194560000121311126 2022-02-22 444003
five 1483160000121315483 2022-02-15 444244
'''
正則過濾
# 過濾出以"訂單"開頭的所有列
res_df = df.filter(regex='^訂單', axis=1)
print(res_df)
'''
訂單號(hào) 訂單日期 訂單編號(hào)
one 2951110000099262111 2022-02-12 444111
two 2181910000909928191 2022-03-02 444122
three 2194560000121355545 2022-01-22 444132
four 1194560000121311126 2022-02-22 444003
five 1483160000121315483 2022-02-15 444244
'''
按index過濾
全名稱過濾
# 過濾出index為"one", "four"的行
res_df = df.filter(items=["one", 'four'], axis=0)
print(res_df)
'''
訂單號(hào) 數(shù)量 價(jià)格(USD) 狀態(tài) 訂單日期 訂單編號(hào)
one 2951110000099262111 92 230 Not Delivered 2022-02-12 444111
four 1194560000121311126 33 190 Not Shipped 2022-02-22 444003
'''
模糊過濾
# 過濾出index包含"e"的所有行
res_df = df.filter(like='e', axis=0)
print(res_df)
'''
訂單號(hào) 數(shù)量 價(jià)格(USD) 狀態(tài) 訂單日期 訂單編號(hào)
one 2951110000099262111 92 230 Not Delivered 2022-02-12 444111
three 2194560000121355545 66 150 Not Delivered 2022-01-22 444132
five 1483160000121315483 15 200 Not Delivered 2022-02-15 444244
'''
正則過濾
# 過濾出以"t"開頭的所有行
res_df = df.filter(regex='^t', axis=0)
print(res_df)
'''
訂單號(hào) 數(shù)量 價(jià)格(USD) 狀態(tài) 訂單日期 訂單編號(hào)
two 2181910000909928191 61 122 Not Delivered 2022-03-02 444122
three 2194560000121355545 66 150 Not Delivered 2022-01-22 444132
'''
多條件過濾
# 過濾出以"價(jià)格"開頭或名稱為"訂單號(hào)"的所有列
res_df = df.filter(regex='^價(jià)格|訂單號(hào)', axis=1)
print(res_df)
'''
訂單號(hào) 價(jià)格(USD)
0 295111000262111 230
1 218191000928191 122
2 21945600355545 150
3 11945600311126 190
4 14831600315483 200
'''
# 過濾出以"t"開頭或以"f"開頭的所有行
res_df = df.filter(regex='^t|^f', axis=0)
print(res_df)
'''
訂單號(hào) 數(shù)量 價(jià)格(USD) 狀態(tài) 訂單日期 訂單編號(hào)
two 2181910000909928191 61 122 Not Delivered 2022-03-02 444122
three 2194560000121355545 66 150 Not Delivered 2022-01-22 444132
four 1194560000121311126 33 190 Not Shipped 2022-02-22 444003
five 1483160000121315483 15 200 Not Delivered 2022-02-15 444244
'''
本文來自博客園,僅供參考學(xué)習(xí),如有不當(dāng)之處還望不吝賜教,不勝感激!轉(zhuǎn)載請(qǐng)注明原文鏈接:http://www.rzrgm.cn/rong-z/p/17610672.html
作者:cnblogs用戶
浙公網(wǎng)安備 33010602011771號(hào)