如何拆分数据框中的值并替换为另一个值

how to split value in dataframe and replace with another value

数据框:

import pandas as pd
d = {'A': ['B_502_ZZZ_01', 'B_400__01']}
df = pd.DataFrame(data=d)

我试过了:

def f(x):
 x = x.split('_')[2]
 if x=='':
   x = x.replace('', 'ZZZ') 
 return x
else: 
 return x
df['A'].apply(f)

输出要求的完整值,不仅是第二名。我可以在另一列中拆分第 3 个位置,但我希望直接在该位置更改位置。

['B_502_ZZZ_01', 'B_400_ZZZ_01'] 

您可以使用正则表达式:

df['A'] = df['A'].str.replace(r'((?:[^_]+_){2})(?=_)', r'ZZZ', regex=True)

注意。正则表达式中的 2 表示执行替换后的 _ 数

输出(为清楚起见作为新列 A2):

              A            A2
0  B_502_ZZZ_01  B_502_ZZZ_01
1     B_400__01  B_400_ZZZ_01

regex demo

def code(x = 'x are values in the columns'):
  '''It will look for 3rd key in the code and fit 'ZZZ' in that place if there is no value in that'''
  
  y = x.split('_')[2]
  z = x.split('_')[3]
  a = x.split('_')[:2]
  if y =='':
    y = y.replace('', 'ZZZ')
    return '_'.join(a + [y] + [z])
  else:
    return x

Df['A'].apply(code)