Home > Blockchain >  Fill DataFrame based on value from another column
Fill DataFrame based on value from another column

Time:11-04

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

CodePudding user response:

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

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

CodePudding user response:

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
  • Related