Chewyham
Chewyham

Reputation: 87

Finding and Modifying duplicated values in an array incrementally (Python)

For a given series of numbers, I would like to find the duplicated entries, and incrementally add 0.1 to each duplicated entry. Here is an example:

nums = pd.Series([1,2,3,4,5,5,5,6,7,8,9,9,10])
#some code here#
print(nums)
0      1
1      2
2      3
3      4
4      5
5      5.1
6      5.2
7      6
8      7
9      8
10     9
11     9.1
12    10
dtype: int64

I figured the first step would be to identify the index at which the numbers are duplicated:

nums.duplicated()
    0     False
    1     False
    2     False
    3     False
    4     False
    5      True
    6      True
    7     False
    8     False
    9     False
    10    False
    11     True
    12    False
    dtype: bool

But I have yet to figure out the next steps, any help would be appreciated.

Upvotes: 1

Views: 32

Answers (1)

jezrael
jezrael

Reputation: 863331

Use GroupBy.cumcount by Series for counter, divide by 10 and add to original Series:

s = nums.add(nums.groupby(nums).cumcount().div(10))
print (s)
0      1.0
1      2.0
2      3.0
3      4.0
4      5.0
5      5.1
6      5.2
7      6.0
8      7.0
9      8.0
10     9.0
11     9.1
12    10.0
dtype: float64

Upvotes: 2

Related Questions