Raghava kattamudi
Raghava kattamudi

Reputation: 87

How do i replace the values of a dataframe on a condition

I have a pandas dataframe with more than 50 columns. All the data except the 1st column is float. I want to replace any value greater than 5.75 with 100. Can someone advise any function to do the same.

The replace function is not working as to_value can only take "=" function, and not the greater than function.

Upvotes: 1

Views: 1159

Answers (5)

Arsal
Arsal

Reputation: 447

This can be done using

df['ColumnName'] = np.where(df['ColumnName'] > 5.75, 100, df['First Season'])

Upvotes: 1

ifly6
ifly6

Reputation: 5331

np.where(df.value > 5.75, 100, df.value)

Upvotes: 0

Alex
Alex

Reputation: 961

import numpy as np
import pandas as pd

#Create df
np.random.seed(0)
df = pd.DataFrame(2*np.random.randn(100,50))

for col_name in df.columns[1:]: #Skip first column
    df.loc[:,col_name][df.loc[:,col_name] > 5.75] = 100

Upvotes: 0

R4444
R4444

Reputation: 2114

If you have a dataframe:

import pandas as pd
import random

df = pd.DataFrame({'first_column': [random.uniform(5,6) for x in range(10)]})

print(df)

Gives me:

   first_column
0  5.620439
1  5.640604
2  5.286608
3  5.642898
4  5.742910
5  5.096862
6  5.360492
7  5.923234
8  5.489964
9  5.127154

Then check if the value is greater than 5.75:

df[df > 5.75] = 100

print(df)

Gives me:

     first_column
0    5.620439
1    5.640604
2    5.286608
3    5.642898
4    5.742910
5    5.096862
6    5.360492
7  100.000000
8    5.489964
9    5.127154

Upvotes: 0

alec_djinn
alec_djinn

Reputation: 10789

You can make a custom function and pass it to apply:

import pandas as pd
import random

df = pd.DataFrame({'col_name': [random.randint(0,10) for x in range(100)]})

def f(x):
    if x >= 5.75:
        return 100
    return x

df['modified'] = df['col_name'].apply(f)
print(df.head())

   col_name  modified
0         2         2
1         5         5
2         7       100
3         1         1
4         9       100

Upvotes: 0

Related Questions