Reputation: 112
I have a dataframe which looks like this:
Province Admissions
Eastern Cape 10
Private 3
Public 7
Free State 20
Private 15
Public 5
I want to change the 'Private' and 'Public' to reference the Province. I want to achieve the following dataframe:
Province Admissions
Eastern Cape 10
Eastern Cape-Private 3
Eastern Cape-Public 7
Free State 20
Free State-Private 15
Free State-Public 5
I've actually already achieved this by the following code:
for row in range(0,len(df)):
df['Province'] = np.where((df['Province'] == 'Private'), df['Province'].shift(1)+' '+ df['Province'], df['Province'])
df['Province'] = np.where((df['Province'] == 'Public'), df['Province'].shift(2)+' '+ df['Province'], df['Province'])
However, I would like to do it in a more general approach, in case the order of the Private and Public is swapped. Right now Private comes before Public hence my method workds. Would appreciate any input!
Upvotes: 1
Views: 57
Reputation: 323376
You can do mask
and ffill
create the adding array
s = df.Province.mask(df.Province.isin(['Private','Public'])).ffill()
df['Province'] = np.where(df.Province.isin(['Private','Public']), s + ' ' + df.Province, df.Province)
Upvotes: 2