1

I would like to replace row values in pandas.

import pandas as pd
import numpy as np
data = {'type': ['place', 'home', 'place', 'walk', 'place', 'work', 'home', 'place'],'labels': ['NaN', 'NaN', 'shop', 'Nan', 'clinic', 'NaN', 'NaN', 'NaN']}
a = pd.DataFrame(data, columns = ['type', 'labels'])

Is there some possibilities to replace a['type'] with a['labels'] only the conditions if a['labels'] is not np.NaN and a['type'] == 'place' using pandas?

I would prefer to use df.loc[] if it is possible.

1 Answer 1

2

First, the data you provided isn't actually np.NaN, it's just the string 'NaN' or 'Nan' which are handled differently. If that is your starting point you can do:

a['labels'] = a['labels'].str.lower().replace('nan', np.NaN)

Then define your mask and replace:

mask = (a['labels'].notnull()) & (a['type'] == 'place') 
df.loc[mask, 'type'] = df.loc[mask, 'labels']

     type  labels
0   place     NaN
1    home     NaN
2    shop    shop
3    walk     NaN
4  clinic  clinic
5    work     NaN
6    home     NaN
7   place     NaN

Alternatively, with the same mask defined above you can use numpy.where

a['type'] = np.where(mask, a['labels'], a['type'])
Sign up to request clarification or add additional context in comments.

1 Comment

you are the best

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.