I have a pandas dataframe like
user_id | music_id | has_rating |
---|---|---|
A | a | 1 |
B | b | 1 |
and I would like to automatically add new rows for each of user_id & music_id for those users haven’t rated, like
user_id | music_id | has_rating |
---|---|---|
A | a | 1 |
A | b | 0 |
B | a | 0 |
B | b | 1 |
for each of user_id and music_id combination pairs those are not existing in my Pandas dataframe yet.
is there any way to append such rows automatically like this?
Advertisement
Answer
You can use a temporary reshape with pivot_table
and fill_value=0
to fill the missing values with 0
:
JavaScript
x
5
1
(df.pivot_table(index='user_id', columns='music_id',
2
values='has_rating', fill_value=0)
3
.stack().reset_index(name='has_rating')
4
)
5
Output:
JavaScript
1
6
1
user_id music_id has_rating
2
0 A a 1
3
1 A b 0
4
2 B a 0
5
3 B b 1
6