computerScience
computerScience

Reputation: 31

Selecting rows and replace columns that have value less than specific value in pandas

I have a dataframe with 30 numeric columns. I would like to select rows that have a value less than, for example, 50 in any column and replace it by the mean of the row. I tried doing the following but it didn't work.

dataset.where((dataset[:]>=50).any(axis=1), dataset.mean(axis=1), axis=1, inplace=True)

Upvotes: 2

Views: 170

Answers (1)

jpp
jpp

Reputation: 164693

You need axis=0 to set a value for all columns.

You also need pd.DataFrame.mask instead of pd.DataFrame.where to change values where the specified criterion is True.

df = pd.DataFrame([[75, 100, 65],
                   [25, 25, 30],
                   [55, 90, 45],
                   [55, 90, 75]])

df.mask((df < 50).any(axis=1), df.mean(axis=1), axis=0, inplace=True)

print(df)

           0           1          2
0  75.000000  100.000000  65.000000
1  26.666667   26.666667  26.666667
2  63.333333   63.333333  63.333333
3  55.000000   90.000000  75.000000

Upvotes: 1

Related Questions