2

I have a dataframe like this:

         desc     id     info  
       [a,b,c]     2     type
       [u,v,w]     18    tail

Three columns: desc,id,info and desc is a list.I want this:

        des    id    info 
         a      2     type
         b      2     type
         c      2     type 
         u      18    tail
         v      18    tail
         w      18    tail

That means exploded the list column into multiple rows and other column no changes. I really don't know how to do this...

宋国庆
  • 93
  • 2
  • 10

4 Answers4

6

Here is one way

df.set_index(['id', 'info']).desc.apply(pd.Series).stack()\
.reset_index(name = 'desc').drop('level_2', axis = 1)


    id  info    desc
0   2   type    a
1   2   type    b
2   2   type    c
3   18  tail    u
4   18  tail    v
5   18  tail    w
Vaishali
  • 37,545
  • 5
  • 58
  • 86
3

I remenber this should be from piRSquared or cᴏʟᴅsᴘᴇᴇᴅ, but can not find the link ...

idx = np.arange(len(df)).repeat(df.desc.str.len(), 0)
out = df.iloc[idx, ].assign(desc=np.concatenate(df.desc.values))
out
Out[100]: 
  desc  id  info
0    a   2  type
0    b   2  type
0    c   2  type
1    u  18  tail
1    v  18  tail
1    w  18  tail
BENY
  • 317,841
  • 20
  • 164
  • 234
2

You can flatten the desc column, repeat the other two columns and then concatenate them:

pd.concat([
    pd.Series([e for s in df.desc for e in s], name='desc'),
    df.drop('desc', 1).apply(lambda col: col.repeat(df.desc.str.len())).reset_index(drop=True)
], axis=1)

#desc   id  info
#0  a    2  type
#1  b    2  type
#2  c    2  type
#3  u   18  tail
#4  v   18  tail
#5  w   18  tail
Psidom
  • 209,562
  • 33
  • 339
  • 356
2

You could

In [1631]: (df.loc[df.index.repeat(df.desc.str.len())]
              .assign(desc=[v for x in df.desc.values for v in x]))
Out[1631]:
  desc  id  info
0    a   2  type
0    b   2  type
0    c   2  type
1    u  18  tail
1    v  18  tail
1    w  18  tail
Zero
  • 74,117
  • 18
  • 147
  • 154