I have this dataframe
A | B | |
---|---|---|
0 | 'a' | ['d1','d2'] |
1 | 'b' | ['e1,'e2','e3'] |
So column A contains strings, column B contains lists (or when I look for type()
: pandas.core.series.series)
That I want to look like:
A | B | B | B | |
---|---|---|---|---|
0 | 'a' | 'd1' | 'd2' | |
1 | 'b' | 'e1 | 'e2' | 'e3' |
I know how to split strings according to a spliting char with df['B'].str.split('<some separator>', <max size of the list within the cell = 3>, expand=True)
but I would like to keep working with lists and not have to convert them to strings first.
I did not find a way to do this, or at least did not managed to modify the found code lines to make it work...
# MWE, the example DataFrame
d = ['d1','d2']; e = ['e1','e2','e3']
df = pd.DataFrame({'A': ['a','b'],'B': [d,e]})
print(type(df[df['A'] == 'a']['B']))
print(df)
Thanks.
CodePudding user response:
You can try this:
val = df['B'].apply(len).max()
cols = ['B' str(i) if i != 0 else 'B' for i in range(val)]
df[cols] = df['B'].apply(pd.Series).fillna('')