Reputation: 35
I have the below df in pandas:
I need to find the duplicate value, in this case the number 1 in the index 2 and apply the formula ma.trunc(ma.pow(x,2))+1 and after replace the value result in the index 2 where the number 1 was.
Any help will be highly appreciated.
Upvotes: 0
Views: 258
Reputation: 466
You can locate all of the duplicates with the duplicated() command. Note that keep="first" (default value) will prevent the first occurence from being marked as a duplicate.
is_dup = df.duplicated(subset="Enrollment", keep="first")
Then you can apply a custom function to the values that are duplicated with the where() and map() functions.
my_func = lambda x: ma.trunc(ma.pow(x,2))+1
df["Enrollment"] = df["Enrollment"].where( ~is_dup, df["Enrollment"].map( my_func ))
Upvotes: 1