4
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

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.