pandas dataframe apply 传入外部参数 args

2022-05-13 12:59:27 浏览数 (1)

代码语言:javascript复制
# !/usr/bin/python3

import pandas as pd

# 如果x小于threshold就等于1,否则等于0
def juege_threshold(x,threshold):
    return 1 if x<=threshold else 0

data_dict={"values":[1,3,5,7,9,11,13,15,17,19]}
data_df=pd.DataFrame(data_dict)
print(data_df)

data_df["values_7"]=data_df["values"].apply(juege_threshold,threshold=7)
data_df["values_10"]=data_df["values"].apply(juege_threshold,threshold=10)
print(data_df)

0 人点赞