Import necessary packages and intialize data:
import pandas as pd
import re
my_list = [['Example1 (purple)',
' Example2 (blue)',
' Example3 (orange)',
' Example4 (yellow)',
' Example5 (red)',
' Example6 (pink)',
' Example7 (sky)'],
['Example8 (purple)',
' Example9 (blue)',
' Example10 (orange)',
' Example11 (sky)',
' Example12 (green)',
' Example13 (green)',
' Example14 (yellow)',
' Example15 (red)',
' Example16 (pink)',
' Example17 (pink)',
' Example18 (green)',
' Example19 (sky)']]
Flatten the list, so its not nested lists in lists. (This is why you got an error that lists don't have split. If you do [x.split() for x in my_list] it will give an error because the elements made up of my_list are lists)
Define a flatlist function and flatten list:
flat_list = lambda l: [item for sublist in l for item in sublist]
flat = flat_list(my_list)
Create an empty dataframe
df = pd.DataFrame({})
Extract out the elements of the single flat list. this strips it of whitespace, then splits it by the space, taking the 0th element for the "Example1", and then strips it again to remove whitespace. do it again but take the 1st element for the color. Wrap it in () and separate by a comma to return it as a tuple.
splitout = [(x.strip().split(' ')[0].strip(), x.strip().split(' ')[1]) for x in pd.Series(flat)]
set the two dataframe columns. the first is just grabbing the first element of the splitout which is always Example, the second uses re.sub to remove the () from the color
df['Example'] = [x[0] for x in splitout]
df['Color'] = [re.sub('[/(/)]', '', x[1]) for x in splitout]
Example Color
0 Example1 purple
1 Example2 blue
2 Example3 orange
3 Example4 yellow
4 Example5 red
5 Example6 pink
6 Example7 sky
7 Example8 purple
8 Example9 blue
9 Example10 orange
10 Example11 sky
11 Example12 green
12 Example13 green
13 Example14 yellow
14 Example15 red
15 Example16 pink
16 Example17 pink
17 Example18 green
18 Example19 sky
Then you can pivot into a larger dataframe with colors for columns:
pd.pivot_table(df.assign(v=1), index='Example', columns='Color', values='v')
Color blue green orange pink purple red sky yellow
Example
Example1 NaN NaN NaN NaN 1.0 NaN NaN NaN
Example10 NaN NaN 1.0 NaN NaN NaN NaN NaN
Example11 NaN NaN NaN NaN NaN NaN 1.0 NaN
Example12 NaN 1.0 NaN NaN NaN NaN NaN NaN
Example13 NaN 1.0 NaN NaN NaN NaN NaN NaN
Example14 NaN NaN NaN NaN NaN NaN NaN 1.0
Example15 NaN NaN NaN NaN NaN 1.0 NaN NaN
Example16 NaN NaN NaN 1.0 NaN NaN NaN NaN
Example17 NaN NaN NaN 1.0 NaN NaN NaN NaN
Example18 NaN 1.0 NaN NaN NaN NaN NaN NaN
Example19 NaN NaN NaN NaN NaN NaN 1.0 NaN
Example2 1.0 NaN NaN NaN NaN NaN NaN NaN
Example3 NaN NaN 1.0 NaN NaN NaN NaN NaN
Example4 NaN NaN NaN NaN NaN NaN NaN 1.0
Example5 NaN NaN NaN NaN NaN 1.0 NaN NaN
Example6 NaN NaN NaN 1.0 NaN NaN NaN NaN
Example7 NaN NaN NaN NaN NaN NaN 1.0 NaN
Example8 NaN NaN NaN NaN 1.0 NaN NaN NaN
Example9 1.0 NaN NaN NaN NaN NaN NaN NaN
whole code:
import pandas as pd
import re
flat_list = lambda l: [item for sublist in l for item in sublist]
flat = flat_list(my_list)
splitout = [(x.strip().split(' ')[0].strip(), x.strip().split(' ')[1]) for x in pd.Series(flat)]
df = pd.DataFrame({})
df['Example'] = [x[0] for x in splitout]
df['Color'] = [re.sub('[/(/)]', '', x[1]) for x in splitout]
pivot = pd.pivot_table(df.assign(v=1), index='Example', columns='Color', values='v')
[[yellow], [yellower]]or[yellow, yellower]?