简体   繁体   中英

Replace column values with NaN if the value on the column next to it is not NaN

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM