I have a column where the values are saved as a dictionary and I used the code below to untangle the values into two separate columns, however, I am struggling with the rows that have Null values (See error msg below): df
product_id product_ratings
2323 {"average_rating": 4.2, "number_of_ratings": 10}
4433 {"average_rating": 4.3, "number_of_ratings": 31}
3454 {"average_rating": 4.5, "number_of_ratings": 23}
4552 {"average_rating": 4.1, "number_of_ratings": 13}
3422 None
desired_output_df
product_id average_rating number_of_ratings
2323 4.2 10
4433 4.3 31
3454 4.5 23
4552 4.1 13
3422 0 0
My code:
import ast
import pandas as pd
df = pd.read_csv('path')
df = df.fillna(0)
dict_df = pd.DataFrame([ast.literal_eval(i) for i in df.product_ratings.values])
df2 = df.drop('product_ratings',axis=1)
final_df = pd.concat([df2,dict_df],axis=1)
final_df
However I am getting the following error: ValueError: malformed node or string: 0