Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace character of column value with string from another column in Pandas

name value_1
dd3   what, _ is
dd4   what, _ is

How to replace '_' from value_1 column with whole string from name column?

Desired output for value_1 column

value_1
what, dd3 is
what, dd4 is

I have tried with this:

df['value_1'] = df['value_1'].apply(lambda x:x.replace("_", df['name']))

And I got this error :expected a string or other character buffer object

like image 372
jovicbg Avatar asked Mar 07 '23 09:03

jovicbg


2 Answers

Use apply with axis=1 for process by rows:

df['value_1'] = df.apply(lambda x:x['value_1'].replace("_", x['name']), axis=1)
print (df)
  name       value_1
0  dd3  what, dd3 is
1  dd4  what, dd4 is
like image 98
jezrael Avatar answered Mar 09 '23 23:03

jezrael


UPDATE: similar to @jezrael's solution, but it should be bit faster for larger data sets (vectorized approach):

In [221]: df['value_1'] = (df.groupby('name')['value_1']
                             .transform(lambda x: x.str.replace('_', x.name)))

In [222]: df
Out[222]:
  name       value_1
0  dd3  what, dd3 is
1  dd4  what, dd4 is

Old answer:

you can create a helper DF:

In [181]: x = df.value_1.str.split('_', expand=True)

In [192]: x
Out[192]:
        0    1
0  what,    is
1  what,    is

then insert a new column into it:

In [182]: x.insert(1, 'name', df['name'])

which yields:

In [194]: x
Out[194]:
        0 name    1
0  what,   dd3   is
1  what,   dd4   is

and replace the original column:

In [183]: df['value_1'] = x.sum(1)

In [184]: df
Out[184]:
  name       value_1
0  dd3  what, dd3 is
1  dd4  what, dd4 is
like image 38
MaxU - stop WAR against UA Avatar answered Mar 09 '23 23:03

MaxU - stop WAR against UA