0

I have a pandas data frame. In one of the columns ('Q8') of this data frame, some of the rows are empty. I would like to replace these empty cells with a string ('ss'). I want to do this replacement with a condition. This condition is that if the string in column ('Q7') is ('I am a student') and the cell in this row at column ('Q8') is empty, replace the empty cell of column ('Q8') with 'ss'.

This is the code which I wrote for it:

for xx in range(0,len(df['Q8'])):
    if df['Q8'][xx]==np.nan:
       if df['Q7'][xx]=='I am a student':
           df['Q8'][xx].replace('', 'ss', regex=True)

but it can not find any np.nan from the first if!!

1 Answer 1

4

Use masking instead:

df.loc[(df['Q7'] == 'I am a student') & (df['Q8'].isna()), 'Q8'] = 'ss'

You can also use fillna:

df.loc[df['Q7'] == 'I am a student', 'Q8'] = df.loc[df['Q7'] == 'I am a student', 'Q8'].fillna('ss')
Sign up to request clarification or add additional context in comments.

4 Comments

@CFD and note the use of the isna() function for finding missing values, which works across different data types.
Why should we put 'Q8' at the end of first method?
@CFD because we need to mask in two dimensions: we want to modify only the cells which belong to rows where df[Q7] has the value 'I am a student' and columns where df[Q8] is null.
@CFD also, the proper syntax for using the .loc indexer is: df.loc[row_indexer,column_indexer].

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.