对dataframe中每个元素进行同样的函数操作,且函数不止一个输入

import numpy as np
import pandas as pd
 
df = pd.DataFrame([[0.01,0.01],[0.2,0.2]])
# df
#       0     1
# 0  0.01  0.01
# 1  0.20  0.20
 
def binarization(x, threshold, pos=1, neg=0):
    if x >= threshold:
        return pos
    else:
        return neg
 
df = df.apply(np.vectorize(binarization),args=(0.1,))
 
 
# df
#    0  1
# 0  0  0
# 1  1  1

你可能感兴趣的:(Python)