when I have empty list in my row I am getting this error:
ValueError: columns must have matching element counts
here is my datframe:
product title variation_list
Chauvet DJ GigBar Move Effect Light System ['Black', 'White']
Pioneer DJ DJM-S11 Professional DJ Mixer []
DJM-S11 Professional DJ []
Pioneer DJ ['black']
dj ['white','blue','red']
When I deleted row of empty list the error gone. How to overcome it?
here is my code:
df[['variation_list']] = df[['variation_list']].applymap(lambda x: eval(x, {'nan': np.nan}))
df = df.explode(['variation_list'])
CodePudding user response:
Use any() to check if list is empy. If the list is empty it will return False, otherwise True. If list is empty, change it to None, otherwise retain the value as a non-empty list
df[['short_des_list']] = df[['short_des_list']].applymap(lambda x: None if any(eval(x)) == False else eval(x))
df[['variation_list']] = df[['variation_list']].applymap(lambda x: None if any(eval(x)) == False else eval(x))
df[['price_list']] = df[['price_list']].applymap(lambda x: None if any(eval(x)) == False else eval(x))
df[['main_image_list']] = df[['main_image_list']].applymap(lambda x: None if any(eval(x)) == False else eval(x))
df = df.explode(['variation_list','short_des_list','price_list','main_image_list'])
df