[英]How to generate Alpha-numaric Fixed length column in panda dataframe
I am trying to create a alpha-numeric(Incremental value) column with fixed length on the basis of one existing column("Number"). 我试图在一个现有列(“数字”)的基础上创建一个固定长度的字母数字(增量值)列。
I have a below data-frame with me: 我跟下面有一个数据框:
Number Space Student
1 MG A
2 FE B
3 GD C
4 MK D
5 OK E
6 OO F
7 PP G
8 QW H
9 WE I
10 ZA J
11 ZQ K
...
100 ZU X
101 ZX Y
102 ZB Z
I need below output (expected output): 我需要低于输出(预期输出):
Number Space Student NUM4
1 MG A P001
2 FE B P002
3 GD C P003
4 MK D P004
5 OK E P005
6 OO F P006
7 PP G P007
8 QW H P008
9 WE I P009
10 ZA J P010
11 ZQ K P011
...
100 ZU X P100
101 ZX Y P101
102 ZB Z P102
In the above output I want to generate New column(NUM4) on the basis of "Number" column but length of new column must be 4.(Alphabet in new column will remain same) 在上面的输出中,我想基于“数字”列生成新列(NUM4),但新列的长度必须为4.(新列中的字母将保持相同)
df['NUM4'] = 'P00' + df['Number']
I am not sure how to introduce a column with fix length which dynamically change as per the "Number" column length. 我不确定如何引入具有修复长度的列,该列根据“Number”列长度动态更改。 could you please help on this? 你能帮忙吗?
Thanks. 谢谢。
Use str.zfill
as: 使用str.zfill
作为:
df['NUM4'] = 'P'+df['Number'].astype(str).str.zfill(3)
print(df)
Number Space Student NUM4
0 1 MG A P001
1 2 FE B P002
2 3 GD C P003
3 4 MK D P004
4 5 OK E P005
5 6 OO F P006
6 7 PP G P007
7 8 QW H P008
8 9 WE I P009
9 10 ZA J P010
10 11 ZQ K P011
熊猫的方式来做到这一点:
df['NUM4'] = df.apply(lambda x: f'P{x.Number:03}', axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.