Home > Mobile >  Convert Dictionary of List of Tuples into Pandas Dataframe
Convert Dictionary of List of Tuples into Pandas Dataframe

Time:05-06

I am a beginning in Python.

I know similar questions have been posed, and I have read through the answers for the past 2 hours, but I can’t seem to get my code to work. Appreciate your help to advise where I might have gone wrong.

I have a dictionary as such:

{Tom: [(“Math”, 98),
(“English”,75)],
Betty: [(“Science”, 42),
(“Humanities”, 15]} 

What is the most efficient way to convert to the following Pandas Dataframe?

Tom Math 98
Tom English 75
Betty Science 42
Betty Humanities 15

I have tried the following method which is throwing up a TypeError: cannot unpack non-iterable int object:

df = pd.DataFrame(columns=[‘Name’,’Subject’,’Score’])
i=0
for name in enumerate(data):
 for subject, score in name:
  df.loc[i]= [name,subject,score]
  i  = 1

Thanks a million!

CodePudding user response:

You can loop and construct a list of list that Pandas can consume.

d = {'Tom': [('Math', 98),
             ('English',75)],
     'Betty': [('Science', 42),
               ('Humanities', 15)]}

data = [[k, *v] for k, lst in d.items() for v in lst]

df = pd.DataFrame(data, columns=['Name','Subject','Score'])
    Name     Subject  Score
0    Tom        Math     98
1    Tom     English     75
2  Betty     Science     42
3  Betty  Humanities     15

CodePudding user response:

Do this,

df = pd.DataFrame(data).melt(var_name = "Name", value_name = "Data")
new_df = pd.DataFrame(df["Data"].tolist(), columns = ["Subject", "Marks"])
new_df.insert(loc = 0, column = "Name", value = df["Name"])

Output -

Name Subject Marks
0 Tom Math 98
1 Betty Science 42
2 Tom English 75
3 Betty Humanities 15
  • Related