更新时间:2022-12-20 11:40:12
您还可以创建一个函数来检查您的条件并将其应用于数据框:
You can also create a function to check your conditions, and apply to the dataframe:
def condition(value):
if 25 <= value <= 35:
return 1
return 0
# stealing sample from @AnandSKumar because I'm lazy
In [32]: df
Out[32]:
age
0 25
1 35
2 76
3 21
4 23
5 30
In [33]: df['age'] = df['age'].apply(condition)
In [34]: df
Out[34]:
age
0 1
1 1
2 0
3 0
4 0
5 1
或使用带有lambda的衬纸:
Or using one liner with lambda:
df['age'] = df['age'].apply(lambda x: 1 if 25 <= x <= 35 else 0)