简体   繁体   中英

How to generate roll numbers in pandas?

I have the following dataframe.

df = pd.DataFrame({'A':['abc1@abc.com','abc2@abc.com','abc3@abc.com','abc4@abc.com','abc2@abc.com','abc3@abc.com'],
                   'B':[4,5,4,5,5,4],
                   })

I need to generate rollnumber for column A in the format

"string+!--10digitnumberstaringfrom1--+string"

If the values are repeated roll number should be unique.

Expected Output:

              A     B  RollNumber
0   abc1@abc.com    4  ABC000000001AB
1   abc2@abc.com    5  ABC000000002AB
2   abc3@abc.com    4  ABC000000003AB
3   abc4@abc.com    5  ABC000000004AB
4   abc2@abc.com    5  ABC000000002AB
5   abc3@abc.com    4  ABC000000003AB

Use list comprehension with zero fill :

#python 3.6+
df['RollNumber'] = [f'ABC{x:010}AB' for x in range(1, len(df) + 1)]
#python 3
#df['RollNumber'] = ['ABC{0:010d}AB'.format(x) for x in range(1, len(df) + 1)]
print (df)

              A  B       RollNumber
0  abc1@abc.com  4  ABC0000000001AB
1  abc2@abc.com  5  ABC0000000002AB
2  abc3@abc.com  4  ABC0000000003AB
3  abc4@abc.com  5  ABC0000000004AB
4   abc2@bc.com  5  ABC0000000005AB
5   abc3@bc.com  4  ABC0000000006AB

EDIT: For same values per column A need factorize with Series.str.zfill :

s = pd.Series(pd.factorize(df['A'])[0] + 1).astype(str).str.zfill(10)
df['RollNumber'] = ('ABC' + s + 'AB')
print (df)
              A  B       RollNumber
0  abc1@abc.com  4  ABC0000000001AB
1  abc2@abc.com  5  ABC0000000002AB
2  abc3@abc.com  4  ABC0000000003AB
3  abc4@abc.com  5  ABC0000000004AB
4  abc2@abc.com  5  ABC0000000002AB
5  abc3@abc.com  4  ABC0000000003AB

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