'Trying to insert a new row to DataFrame results in "TypeError: unhashable type: 'Series'"
I have a DataFrame which looks like this:
id user rating rating2
347 2897196 Delora 1 3
348 1329338 Delora 2 5
349 7065882 Delora 3 1
350 1615477 Delora 4 4
351 2316564 Delora 5 5
Now I want to add a new row to this DF. However I'll get a TypeError: unhashable type: 'Series'
df2 = {'id': movie_id, 'user': 'insert', 'rating': 0, 'rating2': 3}
df = df.append(df2, ignore_index = True)
The problem lies in the following assignment:
df_new = df.append(df2, ignore_index = True) # works, I can print the new DF and I see the added row
df = df_new # <- this breaks my code => TypeError: unhashable type: 'Series'
Can someone explain me what I'm supposed to do to fix it?
Update:
This is the function in which the error occurs:
def get_AB(df_user, df_user2):
df_merged = df_user.copy()
df_merged['rating2'] = 0
for x in df_user2.iterrows():
movie_id = x[1]['id']
if movie_id in df_user['id'].values:
row_id = df_user['id'] == movie_id
df_merged.loc[row_id, 'rating2'] = x[1]['rating']
if movie_id not in df_user['id'].values:
df2 = {'id': movie_id, 'user': 'insert', 'rating': 0, 'rating2': x[1]['rating']}
df = df_merged.append(df2, ignore_index = True)
return 0
The goal is to merge two DataFrames:
# DataFrame 1
id user rating
347 2897196 Delora 1
348 1329338 Delora 2
349 7065882 Delora 3
350 1615477 Delora 4
351 2316564 Delora 5
# DataFrame 2
id user rating
347 12344 Delora 1
348 1329338 Delora 4
To something like this:
id user rating rating2
347 2897196 Delora 1 0
348 1329338 Delora 2 4
349 7065882 Delora 3 0
350 1615477 Delora 4 0
351 2316564 Delora 5 0
xxx 12344 Delora 0 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
