How to rewrite a field of a column in a DataFrame in Python?

Viewed 387

I need to rewrite some fields of a column in a DataFrame, based on a condition.

I have this dataframe:

    Vitamins    Sign
0   B   -
1   C   +
2   A   NaN
3   Z   2
4   E   +
5   I   Expired
6   D   + Severe Cases
7   K   Expired+ Last Year
8   J   +New

And I need to rewrite both columns, based on this condition:

  • if a field from the column 'Sign' contains the sign '+' then it should be copied and pasted on the 'Vitamins' column, on the same row, without any spaces between the last word and the sign. Then, the sign '+' from the 'Sign' column (that field) should be removed.

The result is this dataframe:

Vitamins    Sign
0   B   -
1   C+  NaN
2   A   NaN
3   Z   2
4   E+  NaN
5   I   Expired
6   D   Severe Cases
7   K+  Expired Last Year
8   J+  New

I wrote this code for this:

import pandas as pd
import numpy as np

data = {'Vitamins': ['B', 'C', 'A', 'Z', 'E', 'I', 'D', 'K', 'J'],
       'Sign': ['-', '+', np.nan, 2, '+', 'Expired', '+ Severe Cases', 'Expired+ Last Year', '+New']}

df = pd.DataFrame (data, columns = ['Vitamins', 'Sign'])

mask = (df.loc[:, 'Sign'].str.contains('+', na=False, regex = False))
df['Vitamins'] = str(df.loc[mask, 'Vitamins']) + '+'
df['Sign'] = df.loc[mask, 'Sign'].str.replace('+', '')

But unfortunately it does not do what it is needed.

How can this be resolved?

Lots of Thank You in Advanced!

2 Answers

Use numpy.where:

In [1552]: import numpy as np

In [1553]: df['Vitamins'] = np.where(df['Sign'].str.contains('+', na=False, regex = False), df['Vitamins'] + '+', df['Vitamins'])

In [1557]: df['Sign'] = df['Sign'].replace('+', np.nan).replace('\+', '', regex=True)

In [1558]: df
Out[1558]: 
  Vitamins               Sign
0        B                  -
1       C+                NaN
2        A                NaN
3        Z                  2
4       E+                NaN
5        I            Expired
6       D+       Severe Cases
7       K+  Expired Last Year
8       J+                New

You can select rows by mask and add + first, then in replace is used \+ for escape special rgex character + and replace empty strings to missing values:

mask = (df.loc[:, 'Sign'].str.contains('+', na=False, regex = False))
df.loc[mask, 'Vitamins'] += '+'
#if need also convert to strings
#df.loc[mask, 'Vitamins'] = df.loc[mask, 'Vitamins'].astype(str) + '+'
df['Sign'] = df['Sign'].str.replace('\+', '', regex=True).replace('', np.nan)
print (df)
  Vitamins               Sign
0        B                  -
1       C+                NaN
2        A                NaN
3        Z                  2
4       E+                NaN
5        I            Expired
6       D+       Severe Cases
7       K+  Expired Last Year
8       J+                New
Related