Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Substitution of values in pandas dataframe columns

Tags:

python

pandas

suppose I've a pandas dataframe with column values as age like this 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

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

like image 262
Linus_30 Avatar asked Oct 22 '15 14:10

Linus_30


2 Answers

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:

df['age'] = df['age'].apply(lambda x: 1 if 25 <=  x <= 35 else 0)
like image 157
Anzel Avatar answered Oct 16 '22 16:10

Anzel


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

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
like image 4
Anand S Kumar Avatar answered Oct 16 '22 17:10

Anand S Kumar