I am trying to replace all values in a specific column in a Dataframe if greater than 0. I tried the below but it replace all columns of that specific row.
df.loc[(df['count'] > 0)] = 1
Could anyone assist, thanks..
You are close, need specify column in second parameter:
df.loc[df['count'] > 0, 'count'] = 1
You can also specify multiple columns:
df.loc[df['count'] > 0, ['count', 'another col']] = 1
Another solution is:
df['count'] = np.where(df['count'] > 0, 1, df['count'])
#alternative
#df['count'] = df['count'].mask(df['count'] > 0, 1)
df.loc[(df['count'] > 0), 'count'] = 1
I recommand this tutorial about .loc https://www.youtube.com/watch?v=xvpNA7bC8cs