How can a convert multiple dictionaries into one big dataframe with custom headers?

Advertisements

I have a few dictionaries, and I’d like to put them into one large dataframe with custom columns.

What I have now is two separate dictionaries (I have more but I’ll use two as an example):

dict_team:

{ Team Alpha: linkToAlpha
Team Beta: linkToBeta
Team Charlie: linkToCharlie }

dict_project:

{ Project Delta: linkToDelta
Project Echo: linkToEcho
Project Fox: linkToFox }

I have these two dicts, and I’d like to combine them into one large dataframe (so that I can then turn them into an excel spreadsheet/table) like this:

Team Team Link Project Project Link
Team Alpha linkToAlpha Project Delta linkToDelta
Team Beta linkToBeta Project Echo linkToEcho
Team Charlie linkToCharlie Project Fox linkToFox

I also want the option to not have any table names, and just have a table like the one above (but with no custom headers)

From what I understand, I need to convert all of my dictionaries (they’re already created by a function, and I can reference them by the dict names dict_team and dict_project) into a list, and from there turn that list into one big dataframe.

All of the information I found online used dictionaries that had the keys as the column names, but that’s not what I want.

If anyone can help that would be amazing. Thank you!

>Solution :

You may need to convert the two dictionaries to DataFrame first, and then merge them. Like this:

import pandas as pd

dict_team = {
    "Team Alpha": "linkToAlpha",
    "Team Beta": "linkToBeta",
    "Team Charlie": "linkToCharlie",
}
dict_project = {
    "Project Delta": "linkToDelta",
    "Project Echo": "linkToEcho",
    "Project Fox": "linkToFox",
}
df_team = pd.DataFrame(dict_team.items(), columns=["Team", "Team Link"])
df_project = pd.DataFrame(dict_project.items(), columns=["Project", "Project Link"])
df_merged = pd.concat([df_team, df_project], axis=1)
print(df_merged)

Leave a ReplyCancel reply