The goal is to maintain the relationship between two columns by setting to NaN all the values from one column in another column.
Having the following data frame:
df = pd.DataFrame({'a': [np.nan, 2, np.nan, 4],'b': [11, 12 , 13, 14]})
a b
0 NaN 11
1 2 12
2 NaN 13
3 4 14
Maintaining the relationship from column a
to column b
, where all NaN values are updated results in:
a b
0 NaN NaN
1 2 12
2 NaN NaN
3 4 14
One way that it is possible to achieve the desired behaviour is:
df.b.where(~df.a.isnull(), np.nan)
Is there any other way to maintain such a relationship?
Using fillna() to fill values from another column Here, we apply the fillna() function on “Col1” of the dataframe df and pass the series df['Col2'] as an argument. The above code fills the missing values in “Col1” with the corresponding values (based on the index) from “Col2”.
fillna() method is used to fill NaN/NA values on a specified column or on an entire DataaFrame with any given value. You can specify modify using inplace, or limit how many filling to perform or choose an axis whether to fill on rows/column etc. The Below example fills all NaN values with None value.
You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.
fillna() from the pandas' library, we can easily replace the 'NaN' in the data frame. Procedure: To calculate the mean() we use the mean function of the particular column. Now with the help of fillna() function we will change all 'NaN' of that particular column for which we have its mean.
Using pd.Series.notnull
to avoid having to take the negative of your Boolean series:
df.b.where(df.a.notnull(), np.nan)
But, really, there's nothing wrong with your existing solution.
Another one would be:
df.loc[df.a.isnull(), 'b'] = df.a
Isn't shorter but does the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With