Home > Software design >  Replace column values with NaN if the value on the column next to it is not NaN
Replace column values with NaN if the value on the column next to it is not NaN

Time:04-29

I want to replace some strings in a column with NaN but only if the string on the column next to it (same row) is not NaN. (Python)

My columns look like this:

FOLDER       FILE

nan          03.jpg
services     services
services     services
nan          20190129_145625.jpg
nan          20190129_145627.jpg

So, I want to remove services from the column on the right because is already on the column on the left. I dont want to do it by saying replace with NaN all strings that are no .jpg because I have other strings I'm not showing here that are not jpg.

Thanks

CodePudding user response:

I think you're looking for:

df.loc[~df['FOLDER'].isna(), 'FILE'] = np.nan

Output:

     FOLDER                 FILE
0       NaN               03.jpg
1  services                  NaN
2  services                  NaN
3       NaN  20190129_145625.jpg
4       NaN  20190129_145627.jpg
  • Related