Joey LU
Joey LU

Reputation: 77

How can I replace NaN value with mean in a Pandas dataframe?

I have a dataframe to fill NaN value as follows:

Category           Rating
ART_AND_DESIGN      4.4
AUTO_AND_VEHICLES   4.2
BEAUTY              4.3
BOOKS_AND_REFERENCE 4.3
BUSINESS            4.1
COMICS              4.2
COMMUNICATION       4.2
DATING              4.0
EDUCATION           4.4

I intend to replace the following NaN value with the above Rating based on their Categories.

App   Category    Rating
 A     DATING      NaN (4.0)
 B     BEAUTY      NaN (4.3)
 C     BUSINESS    NaN (4.1)

e.g. Since the dating category has rating 4.0, app A should be filled with 4.0.

Upvotes: 0

Views: 94

Answers (1)

Space Impact
Space Impact

Reputation: 13255

Use df1 after set_index to Category and map the df2['Category'] with df1['Rating'] as:

df1.set_index('Category',inplace=True)
df2['Rating'] = df2['Category'].map(df1['Rating'])

Or using replace:

df2['Rating'] = df2['Category'].replace(df1['Rating'])

print(df2)
  App  Category  Rating
0   A    DATING     4.0
1   B    BEAUTY     4.3
2   C  BUSINESS     4.1

Before mapping the dataframes are:

print(df1)
                     Rating
Category                   
ART_AND_DESIGN          4.4
AUTO_AND_VEHICLES       4.2
BEAUTY                  4.3
BOOKS_AND_REFERENCE     4.3
BUSINESS                4.1
COMICS                  4.2
COMMUNICATION           4.2
DATING                  4.0
EDUCATION               4.4

print(df2)

  App  Category  Rating
0   A    DATING     NaN
1   B    BEAUTY     NaN
2   C  BUSINESS     NaN

Upvotes: 1

Related Questions