Leonardo Ferreira
Leonardo Ferreira

Reputation: 384

Replace values in column based on other column

Using:

import pandas as pd
import numpy as np
a = pd.read_csv('Bvitoria_argos.csv', na_values=[' -99999.0'])

The dataframe is something like that:

HS   Tp
3.0  12.0
2.0  11.3
nan  19.2
nan  5.9
5.6  7.0

The objective is to replace values in ''Tp'' column based on ''HS'' values and get something like that:

HS   Tp
3.0  12.0
2.0  11.3
nan  nan
nan  nan
5.6  7.0

I've tried to use this, but it's not working:

c.loc[c.HS==np.nan,'Tp']=np.nan  

To be more specifc, when is nan in ''HS'' column ''Tp'' column need to be nan to. Would be thankful if someone could help.

Upvotes: 0

Views: 58

Answers (2)

Badri Gs
Badri Gs

Reputation: 340

You could use np.where. If cond is a boolean array, and A and B are arrays, then

C = np.where(cond, A, B)

defines C to be equal to A where cond is True, and B where cond is False.

Compare Indexing where condition.

Upvotes: 0

Scott Boston
Scott Boston

Reputation: 153460

Use isnull():

df.loc[df['HS'].isnull(),'Tp'] = np.nan

Upvotes: 1

Related Questions