如何在熊猫数据框中用均值替换NaN值?

时间:2018-09-28 04:06:14

标签: python pandas

我有一个数据框来填充NaN值,如下所示:

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

我打算根据其类别将以下NaN值替换为上述Rating。

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

例如由于约会类别的评分为4.0,因此应用A应填写4.0。

1 个答案:

答案 0 :(得分:1)

set_index之后将df1用于Category,并将df2['Category']的{​​{3}}与df1['Rating']一起用作:

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

或使用map

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

在映射数据帧之前:

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