Carola
Carola

Reputation: 366

Fill DataFrame based on value from another column

Given the following python pandas dataframe:

province district
Total example
NaN other
Other NaN
NaN example
Result example
NaN example

If the province column is NaN and the value for that row is 'example', I want to fill the province gap with 'example'. The rest of the rows stay as they are.

DataFrame result:

province district
Total example
NaN other
Other NaN
example example
Result example
example example

Upvotes: 0

Views: 46

Answers (2)

ozacha
ozacha

Reputation: 1352

You can use .fillna() conditionally with np.where:

df["province"] = np.where(
    df["district"] == "example", 
    df["province"].fillna(value="example"), 
    df["province"]
)

Upvotes: 1

sophocles
sophocles

Reputation: 13821

You could use loc to find the rows with NaN in your province column and 'example' in your district column and update the values in your province column to be 'example':

df.loc[(df.province.isnull()) & (df.district.eq('example')),'province'] = 'example'

prints:

  province district
0    Total  example
1      NaN    other
2    Other      NaN
3  example  example
4   Result  example
5  example  example

Upvotes: 0

Related Questions