Fill DataFrame based on value from another column

Question:

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
Asked By: Carola

||

Answers:

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
Answered By: sophocles

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

df["province"] = np.where(
    df["district"] == "example", 
    df["province"].fillna(value="example"), 
    df["province"]
)
Answered By: ozacha
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.