Skip to content Skip to sidebar Skip to footer

Pandas: Map Column Using A Dictionary On Multiple Columns

I have a dataframe with None values in one column. I would like to replace this None values with the maximum value of the 'category' for the same combination of other columns. Exam

Solution 1:

two steps,

first lets turn those None values to NaNs so we can use numeric operations.

df['category'] = pd.to_numeric(df['category']) # add arg ,errors='coerce' if needed.

2nd, lets use groupby transform and max to fill only the NaN values.

df["category"] = df["category"].fillna(
    df.groupby(["company", "product"])["category"].transform("max")
)
print(df)

    company    product  category
0  Company1  Product A       1.0
1  Company1  Product A       2.0
2  Company1  Product F       3.0
3  Company1  Product A       2.0
4  Company2  Product F       5.0
5  Company2  Product F       5.0

Post a Comment for "Pandas: Map Column Using A Dictionary On Multiple Columns"