Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make non-unique Pandas column into a unique one

Say I have the following dataframe:

import pandas as pd

df = pd.DataFrame({'Name': ['Jim','Bob','Tim','Sal','Mel'], 
                   'Time': [7,7,7,8,9], 
                   'Value':[15,13,17,6,27]})
Out[1]:
  Name  Time  Value
0  Jim     7     15
1  Bob     7     13
2  Tim     7     17
3  Sal     8      6
4  Mel     9     27

But I want Time to be a unique column in the data, still in numeric form. For example, the series of 7 in Time could be 7.00, 7.01, 7.02 or 7.0, 7.1, 7.2 etc. How could I transform this non-unique numeric Pandas column into a unique one that is still numeric?

like image 228
pete Avatar asked Dec 13 '22 09:12

pete


2 Answers

You can use cumcount:

df['Time'] += df.groupby('Time').cumcount() / 10
    Name    Time    Value
0   Jim     7.0     15
1   Bob     7.1     13
2   Tim     7.2     17
3   Sal     8.0     6
4   Mel     9.0     27
like image 151
Manualmsdos Avatar answered Dec 27 '22 05:12

Manualmsdos


Add random data

df.assign(Time=df.Time.add(np.random.rand(len(df)) / 10).round(2))

  Name  Time  Value
0  Jim  7.07     15
1  Bob  7.01     13
2  Tim  7.05     17
3  Sal  8.00      6
4  Mel  9.03     27
like image 42
piRSquared Avatar answered Dec 27 '22 07:12

piRSquared