0

I have the following dataframe:

col1   col2
[12, 2]  4
[2, 5]  3

I want this:

col1A   col1B   col2
12        2       4
2        5       3

This question is similar to this but on doing df['col1'].str[0], I'm getting this as output:

col1   col2
[       4
[       3
user2991421
  • 387
  • 2
  • 4
  • 14

2 Answers2

2

Use:

new_df=df.T.apply(lambda x: x.explode()).T
print(new_df)
  col1 col1 col2
0   12    2    4
1    2    5    3

If you want rename the column use:

df2=df.T.apply(lambda x: x.explode())
groups=df2.groupby(level=0)
letters=groups.cumcount().map({0:'A',1:'B'})
df2.index=(df2.index+letters).where(groups.size()>1,df2.index.to_series())
new_df=df2.T
print(new_df)

  col1A col1B col2
0    12     2    4
1     2     5    3
ansev
  • 30,322
  • 5
  • 17
  • 31
  • Thank you for the answer. However, the solution didnt work for my case. The new_df is same as df. I tried initializing another dummy dataframe and your solution worked. Not sure where the problem is. could it be a problem with how data was read from csv? – user2991421 Nov 17 '19 at 02:24
  • 1
    @user2991421 Please share _all_ the relevant code and data. See: [mcve]. – AMC Nov 17 '19 at 08:01
  • If this code works with an example but not with your csv file, it may be a problem of how you open the file and have nothing to do with this problem. Therefore I think the answer is still correct – ansev Nov 17 '19 at 10:04
1

I will do

df=pd.DataFrame(df.pop('col1').tolist(),index=df.index).join(df)
Out[41]: 
    0   1  col2
0   1   2     1
1  10  11     2
BENY
  • 317,841
  • 20
  • 164
  • 234