I have a dataframe with a column contain multiple values. I need to split the values and assigned them to separate but matching columns. Example Fruits column contain different fruits. I need to splits these fruits and assign apple to apple column, banana to banana column ..etc.
fruits 0 apple/mango 1 grapes 2 orange/apple 3 grapes/orange/apple/banana 4 banana/grapes/apple I created data frame but when I split values in a row ,these values are added to multiple rows instead of single row. There should be 5 rows in new data frame but I'm getting 12 rows
columns_name =['apple','banana','grapes','orange','mango'] fruits =df['fruits'].to_list() columnlists_list =[] for column in columns_name: column_list = column column_list =[] columnlists_list.append(column_list) for fruit in fruits: one_fruit = fruit.split('/') for i in range(0,len(one_fruit)): if column == one_fruit[i]: column_list.append('1') else: column_list.append('0') #data frame fruits_df = pd.DataFrame(columnlists_list) fruits_df =fruits_df.transpose() fruits_df.columns =[columns_name] This the result I got.
apple banana grapes orange mango 0 1 0 0 0 0 1 0 0 0 0 1 2 0 0 1 0 0 3 0 0 0 1 0 4 1 0 0 0 0 5 0 0 1 0 0 6 0 0 0 1 0 Please advise how to correct the error. Thank you