Reputation: 327
I am working with a dataframe that looks something like
duration count
0 23
2 12
0 43
2 12
0 43
4 65
1 22
I simply want to replace all of the 0's in the duration column with 1. I tried to do this by
df_sorted = df_sorted.replace(df_sorted['duration'] == 0, df_sorted['duration'] == 1)
However, I run this and the dataframe seems unaffected. I do not get an error message and nothing seems to change. I am working with a much bigger dataframe with a lot more rows/columns so maybe I'm missing that something else is changing? Regardless, the 0's still remain after I run the line of code.
Upvotes: 3
Views: 66
Reputation: 5140
You are missing inplace=True
which essentially make the change permanent.
Note: inplace=True
returns None inplace=False
returns a copy of the object with the operation performed.
df_sorted = pd.DataFrame({'duration':[1,0,2,0],'Count':[23,23,2,1]})
df_sorted.replace({'duration': {0:1}}, inplace=True)
print(df_sorted)
Upvotes: 0
Reputation: 294258
df_sorted['duration'] += df_sorted['duration'].eq(0)
df_sorted
duration count
0 1 23
1 2 12
2 1 43
3 2 12
4 1 43
5 4 65
6 1 22
Upvotes: 3
Reputation: 18367
You can use np.where
which is somewhat similar to a case when
from sql.
df['duration'] = np.where(df['duration'] == 0,1,df['duration'])
Which reads as: Check the value of each row of df['duration']
, if it's equal to 0, replace it with a 1, otherwise, return df['duration']
Upvotes: 1