简体   繁体   中英

How to generate the unique id from a list of ids containing duplicates

I am using pandas package to deal with my data, and I have a dataframe looks like below.

data = pd.read_csv('people.csv')
id, A, B
John, 1, 3
Mary, 2, 5
John, 4, 6
John, 3, 7
Mary, 5, 2

I'd like to produce the unique id for those duplicates but keep the same order of them.

id, A, B
John, 1, 3
Mary, 2, 5
John.1, 4, 6
John.2, 3, 7   # John shows up three times.
Mary.1, 5, 2   # Mary shows up twice.

I tried something like set_index , pd.factorize() and index_col but they do not work.

In order to obtain the indices you may use GroupBy.cumcount :

>>> idx = df.groupby('id').cumcount()
>>> idx
0    0
1    0
2    1
3    2
4    1
dtype: int64

The non-zero ones may be appended by:

>>> mask = idx != 0
>>> df.loc[mask, 'id'] += '.' + idx[mask].astype('str')
>>> df
       id  A  B
0    John  1  3
1    Mary  2  5
2  John.1  4  6
3  John.2  3  7
4  Mary.1  5  2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM