Python 如何在熊猫中生成卷号?
我有以下数据帧Python 如何在熊猫中生成卷号?,python,pandas,Python,Pandas,我有以下数据帧 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], }) 我需要以以下格式为列A生成rollnumber “字符串+!--10digitnumberstaringfrom1-->strin
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],
})
我需要以以下格式为列A生成rollnumber
“字符串+!--10digitnumberstaringfrom1-->string”
如果值重复,卷号应唯一。
预期输出:
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
将列表理解用于: 编辑:对于每列的相同值
A
需要:
你能添加预期的输出和代码吗?你尝试了什么?@jezrael检查一下now@what如果我有重复的值,现在检查预期的输出如何使用df['RollNumber']=[f'ABC{x:010}AB'表示范围(1,len(df)+1)内的x]此代码从起始卷号开始打印20@Yog-使用
df['RollNumber']=[f'ABC{x:010}AB'表示范围(20,len(df)+21)内的x]
geeting此错误值错误:值的长度与值的长度不匹配index@Yog-ooops,未测试-需要df['RollNumber']=[f'ABC{x:010}AB'用于范围(20,len(df)+20)内的x]
#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
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