Home > Software design >  Pandas: Replace pattern in column values with list values
Pandas: Replace pattern in column values with list values

Time:08-03

Stuck on the following in Pandas... I have the dataframe:

df = pd.DataFrame({'col1': ['aaCHANGEMEbb', 'nnANDMEdd', 'hhMETOOkk'], 'index': ['a', 'b', 'c']}).set_index('index')

           col1
a  aaCHANGEMEbb
b     nnANDMEdd
c      hhXXXXkk

And I want to change all uppercase cases in column 'col1' with values from m_list:

m_list = ['1.0', '2.0', '3.0']

One of my attempt that seems as something that is near to truth:

df['col1'] = df['col1'].str.replace('[A-Z] ', lambda x: [i for i in m_list], regex=True)

And another one:

df['col1'].str.replace('([A-Z] )', lambda x: m_list[x.group(0)])

It doesn't work. I got this in both cases:

   col1
a   NaN
b   NaN
c   NaN

But the expected df below:

         col1
a     aa1.0bb
b     nn2.0dd
c     hh3.0kk

Please, share your thoughts about this. Thanks!

CodePudding user response:

You can do with split

s = df['col1'].str.split(r'([A-Z] )',expand=True)
s.loc[:,1] = m_list
df['col1'] = s.agg(''.join,1)
df
Out[255]: 
          col1
index         
a      aa1.0bb
b      nn2.0dd
c      hh3.0kk

CodePudding user response:

for idx, x in enumerate(m_list):
    df.loc[idx] = re.sub(r'([A-Z] )', x, df.loc[idx][0])
  • Related