Tim
Tim

Reputation: 45

Replace only numeric values

I have to replace only numeric values in column C to "profit", if C.values is greater than 50

I have the following code but it replaces everything, not just the numeric values

df['C'].values[df['C'].values >= 50] = 'Profit'

My dataframe

A       B       C
test    NaN     xyz
hit     NaN     10
hit     NaN     90
hit     NaN     abc
test    val     20
test    val     90

My output

A       B       C
test    NaN    Profit
hit     NaN     10
hit     NaN     Profit
hit     NaN     Profit
test    val     20
test    val    Profit

My desired output

A       B       C
test    NaN    xyz
hit     NaN     10
hit     NaN     Profit
hit     NaN     abc
test    val     20
test    val     Profit

Upvotes: 0

Views: 293

Answers (3)

Kai Aeberli
Kai Aeberli

Reputation: 1220

A more terse solution could be:

df['C'][df['C'].apply(lambda x: x > 50 if isinstance(x, int) else False)] = 'Profit'

Upvotes: 1

Valentino
Valentino

Reputation: 7361

You could iterate over the dataframe rows and edit each line when needed:

for idx, row in df.iterrows():
    try:
        if int(row['C']) >= 50:
            row['C'] = 'Profit'
    except ValueError:
        pass

From pandas docs:

df.iterrows()
Iterate over DataFrame rows as (index, Series) pairs.

Upvotes: 0

Jonathan Nelson
Jonathan Nelson

Reputation: 124

I would use .apply() here, with both a try-except and an if-else block.

Something like:

def convert_to_profit(value):
    try:
        if int(value) >= 50:
            return 'Profit'
        else:
            return value
    except ValueError:
        return value

df.loc[:, 'C'] = df['C'].apply(convert_to_profit)

The try-except block will allow you to catch non-numeric values and return the non-replaced value.

Upvotes: 3

Related Questions