Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- df=pd.DataFrame(data=[[301,301,302,303],[['a'],['b','c'],['e','f',33,'Z'],42],index=['id','foo']).T
- id foo
- 0 301 [a]
- 1 301 [b, c]
- 2 302 [e, f,33,'Z']
- 3 303 42
- id foo
- 0 301 a
- 1 301 b
- 1 301 c
- 2 302 e
- 2 302 f
- 2 302 33
- 2 302 Z
- 3 303 42
- df['foo'] = [x if isinstance(x, list) else [x] for x in df['foo']]
- from itertools import chain
- df = pd.DataFrame({
- 'id' : df['id'].repeat(df['foo'].str.len()),
- 'foo' : list(chain.from_iterable(df['foo'].tolist()))
- })
- print (df)
- id foo
- 0 301 a
- 1 301 b
- 1 301 c
- 2 302 e
- 2 302 f
- 2 302 33
- 2 302 Z
- 3 303 42
- df.set_index('id')['foo'].apply(pd.Series).stack().reset_index(name='foo').drop('level_1', axis=1)
- id foo
- 0 301 a
- 1 301 b
- 2 301 c
- 3 302 e
- 4 302 f
- 5 302 33
- 6 302 Z
- 7 303 42
- id test foo
- 0 301 1 [a]
- 1 301 2 [b, c]
- 2 302 3 [e, f, 33, Z]
- 3 303 4 42
- df.set_index(['id','test'])['foo'].apply(pd.Series).stack().reset_index(name='foo').drop('level_2', axis=1)
- id test foo
- 0 301 1 a
- 1 301 2 b
- 2 301 2 c
- 3 302 3 e
- 4 302 3 f
- 5 302 3 33
- 6 302 3 Z
- 7 303 4 42
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement