Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have a list of files in a folder in my system

file_list= ["A", "B", "C"]

I Have read the files using a for loop and have obtained content which is as follows

A = ["A1", "B1", "C1"]

B = ["E1", "F1"]

C = []

I would like the following output

    Content  Name

      A1     A

      B1     A

      C1     A

      D1     B

      E1     B

             C

How do I accomplish this?

1 Answer

0 votes
by (36.8k points)

Try this

import pandas as pd

data = list(zip((A, B, C), file_list))

df = pd.DataFrame(data, columns=['Content', 'Name'])

df = df.explode('Content')

print(df)

Output:

  Content Name

0 A1 A

0 B1 A

0 C1 A

1 E1 B

1 F1 B

2 NaN C

 Learn Data Science Course to improve your technical knowledge.

Browse Categories

...