0

Consider the table (Dataframe) below.

enter image description here

Need each item in the list against its index such as given below. What are the possible ways of doing this in python?

enter image description here

Anybody can tweak the question if it matches the context.

Aniket Ghole
  • 192
  • 1
  • 11
  • 1
    Does this answer your question? [Pandas expand rows from list data available in column](https://stackoverflow.com/questions/39011511/pandas-expand-rows-from-list-data-available-in-column) Specifically the 2nd answer – Karan Shishoo Jan 08 '20 at 05:42

1 Answers1

1

You can do this using the pandas library with the explode method. Here is how your code would look -

import pandas as pd

df = [["A", [1,2,3,4]],["B",[9,6,4]]]

df =  pd.DataFrame(df, columns = ['Index', 'Lists'])

print(df)

df = df.explode('Lists').reset_index(drop=True)

print(df)

Your output would be -

  Index         Lists
0     A  [1, 2, 3, 4]
1     B     [9, 6, 4]


  Index Lists
0     A     1
1     A     2
2     A     3
3     A     4
4     B     9
5     B     6
6     B     4
Karan Shishoo
  • 2,402
  • 2
  • 17
  • 32