Home > Blockchain >  Replacing values from a pandas dataframe
Replacing values from a pandas dataframe

Time:10-30

I have a data frame which have columns with strings and integers.

df = pd.DataFrame([ ['Manila', 5,12,0], ['NY',9,0,14], ['Berlin',8,10,6] ], columns = ['a','b','c','d'])

I want to change all the values to "1" where the value is greater than 1 and the zeros will be reamin the same.

So I tried with apply(lambda x: 1 if x > 1 else 0) but it shows its ambigious.

Then I tried to write a function separately as follow:

def find_value(x):

    try:
        x = int(x)
        print(x)
        if x > 1:
            x = 1
        else:
            x = 0
    except:
        return x
    return x

and then apply it

df = df.apply(find_value, axis=1)

But the output does not change and the df remains as it was.

I think there should be some apply function which can be applied on all of the eligible columns (those columns which has numerical values). But I am missing the point somehow. Can anyone please enlighten me how to solve it (with or without "map" function)?

CodePudding user response:

Use DataFrame.select_dtypes for get numbers columns, compare for greater like 1 and then map True, False to 1,0 by casting to integers, for change data in original is used DataFrame.update:

df.update(df.select_dtypes(np.number).gt(1).astype(int))
print (df)
        a  b  c  d
0  Manila  1  1  0
1      NY  1  0  1
2  Berlin  1  1  1

Or use DataFrame.clip if all integers and no negative numbers:

df.update(df.select_dtypes(np.number).clip(upper=1))
print (df)
        a  b  c  d
0  Manila  1  1  0
1      NY  1  0  1
2  Berlin  1  1  1

EDIT:

Your solution working with DataFrame.applymap:

df = df.applymap(find_value)
  • Related