我正在處理 20 個左右的資料幀,每個資料幀都有不同的列名,并且在每個資料幀中,如果有一個大于 x 的數值,我想減去 x。
例如,在 df 一
Name ID DOB
Joe Smith 10000000021021 01011901
Jane Smith 10000001074600 07025901
但是然后在 df 2 我可能有
Group Office OrderNum
20151 10000000060021 20051568
70386 90021 20051568
并且這些列名稱在每個 df 中都不同,但如果數值大于 10000000000000,則減去該數量
uj5u.com熱心網友回復:
你可以使用applymap:
THRESH = 10000000000000
df.applymap(lambda x: x-THRESH if isinstance(x, (int, float)) and x>THRESH else x)
示例輸出:
Name ID DOB
Joe Smith 21021 1011901
Jane Smith 1074600 7025901
uj5u.com熱心網友回復:
您可以使用select_dtypes和處理這些列:
num = 1e13
dfs = [df1, df2]
for i in dfs:
cols = i.select_dtypes("int64").columns # or use 'number'
i[cols] = i[cols].mask(i[cols]>num, i[cols]-num)
print (i)
Name ID DOB
0 Joe Smith 21021 1011901
1 Jane Smith 1074600 7025901
Group Office OrderNum
0 20151 60021 20051568
1 70386 90021 20051568
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/316887.html
上一篇:【有獎投票】程式員IT好書評選
