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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With