Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- toConvert = pd.DataFrame({
- 'x': [1, 2],
- 'y': [10, 20],
- 'z': [(101, 102, 103), (201, 202)]
- })
- def flatten(df):
- tmp = []
- def backend(r):
- x = r['x']
- y = r['y']
- zz = r['z']
- for z in zz:
- tmp.append({'x': x, 'y': y, 'z': z})
- df.apply(backend, axis=1)
- return pd.DataFrame(tmp)
- print(flatten(toConvert).to_string(index=False))
- x y z
- 1 10 101
- 1 10 102
- 1 10 103
- 2 20 201
- 2 20 202
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement