Reputation: 723
I have a dataframe which looks like -
data = {'col_0': ['a', 'a', 'a', 'a', 'b','b','b'], 'col_1': [-2, -7, 6, 8, -5, 2, 6]}
df = pd.DataFrame(data)
df
col_0 col_1
a -2
a -7
a 6
a 8
b -5
b 2
b 6
What I want is to clip the values of col_1 between -2 to 2 if col_0 is a.
Things that I have tried till now are -
df.loc[df['col_0']=='a',"col_1"].clip(lower = -2, upper = 2,inplace=True)
df.loc[df['col_0']=='a',"col_1"] = df.loc[df['col_0']=='a',"col_1"].clip(lower = -2, upper = 2)
Upvotes: 0
Views: 698
Reputation: 723
df.loc[df['col_0']=='a',"col_1"] = df.loc[df['col_0']=='a',"col_1"].clip(lower = -2, upper = 2)
Upvotes: 1