I am learning python and working on a sample Kaggle dataset and trying to split comma-separated values in a column into different columns using python or pandas in jupyter notebook.
For instance :
column_A
Garbage: Tissues, Organics: Milk, Recycle: Cardboards
Garbage: Paper Towels, Organics: Eggs, Recycle: Glass, Junk: Feces
Garbage: cups, Recycle: Plastic bottles
I want to split these into different columns based on commas, like below:
| Garbage | Organics | Recycle | Junk |
|---|---|---|---|
| Tissues | Milk | Cardboards | Null |
| Paper Towels | Eggs | Glass | Feces |
| Cups | Null | Plastic bottles | Null |
I've tried using Lambda functions but it only worked if there is same length of comma separated strings but not for unequal length and displaying an index error "list index out of range". The code I've used is below:
list_of_dicts = [{x1.split(':')[0].strip():x1.split(':')[1].strip() for x1 in x.split(',')} for x in Df1['column_name']]
Df2=pd.DataFrame.from_dict(list_of_dicts)
Any help is greatly appreciated. Thanks