Skip to main content
 首页 » 编程设计

python之修改 Pandas Series 列中的所有值

2025年05月04日12shangdawei

我有一列包含 9 个字符的长数字。我需要对该列中的所有值执行一些操作以达到 12 的长度。这是原始数据:

493    123456789 
494    123456789 
496    115098765 
497    123456789 
498    987654321 
499    987654321 

现在,我需要对数字进行一些修改:

  • 在第1个之后,需要插入20个
  • 在最后5个数字之前,需要插入一个0

所需的解决方案是:

493    120234056789 
494    120234056789 
496    120150098765 
497    120234056789 
498    920876054321 
499    920876054321 

我该怎么做? 提前致谢。

请您参考如下方法:

将 indexing with str 用于切片值:

s = df['col'].astype(str) 
df['new'] = s.str[0] + '20' + s.str[1:-5] + '0' + s.str[-5:] 
print (df) 
           col           new 
493  123456789  120234056789 
494  123456789  120234056789 
496  115098765  120150098765 
497  123456789  120234056789 
498  987654321  920876054321 
499  987654321  920876054321 

apply 的类似解决方案:

df['new'] = df['col'].astype(str).apply(lambda x:x[0] + '20' + x[1:-5] + '0' + x[-5:]) 

@Mark Wang 的表现:

#6k rows    
df = pd.concat([df] * 1000, ignore_index=True) 
 
In [241]: %%timeit 
     ...: s = df['col'].astype(str) 
     ...: df['new1'] = s.str[0] + '20' + s.str[1:-5] + '0' + s.str[-5:] 
     ...:  
19.5 ms ± 1.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 
 
In [242]: %%timeit  
     ...: df['new2'] = df['col'].astype(str).apply(lambda x:x[0] + '20' + x[1:-5] + '0' + x[-5:]) 
     ...:  
11.4 ms ± 120 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) 

第二个更快,因为 pandas 文本函数更慢。原因之一是,它们可以正确处理缺失值。