Khutso Mphelo
Khutso Mphelo

Reputation: 63

Fill null values with previous string from the same column using condition

I have a dataframe with missing values on one column and I am trying to fill those missing values with the previous string from the same column but given that Another column == something. image

for instance I want to populate missing values under 'Name' with the previous string only for those rows where Business Unit='GTI Shared S2D'

let's say we have a dataframe like this

Name Part Size(GB) Business Unit

579 srv005831 120.0 GTI Shared S2D
580 Nan 50.0 GTI Shared S2D
581 Nan 30.0 GTI Shared S2D
582 srv000711 120.0 GTI Shared S2D
583 Nan 50.0 GTI Shared S2D

584 Nan 20 Call centre

the output should look like this

Name Part Size(GB) Business Unit

579 srv005831   120.0   GTI Shared S2D  <br/>
580 srv005831   50.0    GTI Shared S2D   <br/> 
581 srv005831           30.0    GTI Shared S2D <br/> 
582 srv000711   120.0   GTI Shared S2D   <br/>
583 srv000711   50.0    GTI Shared S2D        <br/> 
584  Nan         20      Call centre     <br/>

Upvotes: 0

Views: 132

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34086

You can use ffill():

x[x['Business Unit'] == 'GTI Shared S2D']['Name'].ffill()

Take below df for example:

In [331]: df = pd.DataFrame({'Name':['srv005831', 'srv005632', np.nan, np.nan], 'Part Size(GB)':[120.0, 50.0, 30.0, 120.0], 'Business Unit':['GTI Shared S2D', 'Call Center', 'GTI Shared S2D', 'GTI Shared S2D']})

In [332]: df
Out[332]: 
        Name  Part Size(GB)   Business Unit
0  srv005831          120.0  GTI Shared S2D
1  srv005632           50.0     Call Center
2        NaN           30.0  GTI Shared S2D
3        NaN          120.0  GTI Shared S2D

My code runs fine:

In [339]: ix = df[df['Business Unit'] == 'GTI Shared S2D'].index.tolist()

In [346]: df.loc[ix] = df.loc[ix]['Name'].ffill()

In [347]: df
Out[347]: 
        Name Part Size(GB) Business Unit
0  srv005831     srv005831     srv005831
1  srv005632            50   Call Center
2  srv005831     srv005831     srv005831
3  srv005831     srv005831     srv005831

Upvotes: 0

Related Questions