Python: Assign missing value to rows in one column if any row is missing value in other columns

Beginner in the house

I have dataframe where column 'Score' is calculated from values in other columns. I would need to have missing value in Score column if any of other columns has missing value for that row.

df = pd.DataFrame({'Score': [71, 63, 23],
                   'Factor_1': [nan, '15', '23'],
                   'Factor_2': ['12', nan, '45'],
                   'Factor_3': ['3', '5', '7']})

Expected values for column Score: nan, nan, 23 (because Factor 1 is missing in 1st row and Factor 2 is missing in 2nd row). So, I should replace existing values with NAs.

Thank you for your help.

jezrael

Use DataFrame.filter for Factor column, test if missing values by DataFrame.isna for at least one value per row by DataFrame.any and set NaN by DataFrame.loc:

df.loc[df.filter(like='Factor').isna().any(axis=1), 'Score'] = np.nan

Or use Series.mask:

df['Score'] = df['Score'].mask(df.filter(like='Factor').isna().any(axis=1))

If need explicit columns names:

cols = ['Factor_1', 'Factor_2', 'Factor_3']
df.loc[df[cols].isna().any(axis=1), 'Score'] = np.nan

df['Score'] = df['Score'].mask(df[cols].isna().any(axis=1))

print (df)
   Score Factor_1 Factor_2 Factor_3
0    NaN      NaN       12        3
1    NaN       15      NaN        5
2   23.0       23       45        7

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Adding a row for missing column value, with all other column values as NaNs

How to check one rows value present in any of the other column row value

Subsetting all Rows with NA (Missing Value) in any of the columns

How to convert rows that contain same value for one column but different for other columns into one single row using R?

Return rows where one dataframe has value and other is missing

Add column and fill missing value based on other columns in Pandas

Delete rows with duplicate value in one column but different value in other columns

How can I remove a row from csv file with python where a value of any column is missing

Python: Set new column value if multiple columns in a dataframe have any value other than one specific string

Insert missing value based on other rows

Splitting value from one row to other rows in the same column in pandas

fill missing value base on other columns pandas

Python: Assign value to a new column in Pandas as list using other columns

For each row check if value in one column exists in two other columns

Replicating value from one column into other columns based on row content

Replace value in one column if another column is missing

Adding rows with value "0" for missing rows in python

Adding values for missing rows based on a column value

Is there any way to replace a missing value based on another columns' value to match the column name

insert missing rows in a Dataframe and fill with previous row values for other columns

fill missing value based on one column to another

If a value of one column appears in any of three other columns, change the value of the respective column to NA

Compare value in a specific row and column with the max value from a range in other rows and columns

average column rows according to value in other columns

How to fill missing value based on other columns in Pandas based on an interval in another column?

Replace all missing value based on other column value

Pandas: groupby and assign all other rows a value from a single specific row and column

Add missing value in column according to value of previous or next row in R

Fill missing value based on value from another column in the same row

TOP Ranking

HotTag

Archive