我有一个数据框来填充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。
答案 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