pandas 数据框列中值的条件替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33283579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:05:58  来源:igfitidea点击:

Conditional Substitution of values in pandas dataframe columns

pythonpandas

提问by Linus_30

suppose I've a pandas dataframe with column values as age like this df.age = {25, 35, 76, 21, 23, 30}

假设我有一个 Pandas 数据框,列值是这样的 df.age = {25, 35, 76, 21, 23, 30}

I want to do an inplace replace like this:

我想做这样的就地替换:

if df.age >=25 and df.age <= 35: replace that value with 1 else: replace that value with 0

如果 df.age >=25 和 df.age <= 35:将该值替换为 1 否则:将该值替换为 0

I've tried this df[df.age >= 7.35 and df.age <= 7.45, 'age'] = 0 but doesn't seem to work.

我试过这个 df[df.age >= 7.35 and df.age <= 7.45, 'age'] = 0 但似乎不起作用。

回答by Anzel

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


Or using one liner with lambda:

或者使用一个带有 lambda 的衬垫:

df['age'] = df['age'].apply(lambda x: 1 if 25 <=  x <= 35 else 0)

回答by Anand S Kumar

You can compare the series with the values (25/35) according to your condition, and then use astype(int)to convert the True/Falsevalues, to 1/0. Example -

您可以根据您的条件将系列与值 (25/35) 进行比较,然后使用astype(int)True/False值转换为1/0. 例子 -

df['age'] = ((25 <= df['age']) & (df['age'] <= 35)).astype(int)

Demo -

演示 -

In [2]: df = pd.DataFrame([[25], [35], [76], [21], [23], [30]],columns=['age'])

In [3]: df
Out[3]:
   age
0   25
1   35
2   76
3   21
4   23
5   30

In [6]: ((25 <= df['age']) & (df['age'] <= 35)).astype(int)
Out[6]:
0    1
1    1
2    0
3    0
4    0
5    1
Name: age, dtype: int32