我正在处理20个左右的数据帧,每个数据帧都有不同的列名,在每个数据帧中,如果有一个数值大于x,我想减去x。
例如,在df one
中Name ID DOB
Joe Smith 10000000021021 01011901
Jane Smith 10000001074600 07025901
但是在df 2中我可能有
Group Office OrderNum
20151 10000000060021 20051568
70386 90021 20051568
和这些列名在每个df中是不同的,但如果一个数值大于10000000000000,则减去该数值
您可以使用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
您可以使用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