Home > Enterprise >  Nested List to Dictionary and back again
Nested List to Dictionary and back again

Time:07-26

I have some data structured like this:

[["ID", "Header1", "Header2"], [1, "a1", "a2"], [2, "b1", "b2"]]

I want to be able to convert this to a dictionary (or maybe pandas dataframe?) that looks like this (or has constant lookup for ID).

{
  1: {
    "Header1": "a1",
    "Header2": "a2",
  },
  2: {
    "Header1": "b1",
    "Header2": "b2",
  }
}

But here is the tricky part.... after doing some processing (lets say adding a 3rd id and dictionary), I need to be able to convert it back to a nested list with the headers in the same order.

[["ID", "Header1", "Header2"], [1, "a1", "a2"], [2, "b1", "b2"], [3, "c1", "c2"]]

Is there a simple way of doing this in pandas? Or is dictionary the way to go?

CodePudding user response:

With you can use:

l = [["ID", "Header1", "Header2"], [1, "a1", "a2"], [2, "b1", "b2"]]

d = pd.DataFrame(l[1:], columns=l[0]).set_index('ID').to_dict('index')

print(d)

df = pd.DataFrame.from_dict(d, 'index').rename_axis('ID').reset_index()
l2 = [list(df.columns)] df.values.tolist()

print(l2)

Output:

{1: {'Header1': 'a1', 'Header2': 'a2'},
 2: {'Header1': 'b1', 'Header2': 'b2'}}

[['ID', 'Header1', 'Header2'],
 [1, 'a1', 'a2'],
 [2, 'b1', 'b2']]

A one liner version of the conversion to list (a bit hacky):

l2 = pd.DataFrame.from_dict(d, 'index').rename_axis('ID').reset_index().T.reset_index().T.values.tolist()

CodePudding user response:

This can do the job -

df = pd.DataFrame(data = data[1:], columns = data[0]).T
df.columns = df.loc["ID"]
print(df.drop(index = "ID").to_dict()) # To print the dictionary
print(df.reset_index().values.tolist()) # To print the nested list

Output -

{1: {'Header1': 'a1', 'Header2': 'b1'}, 2: {'Header1': 'a2', 'Header2': 'b2'}}

[['ID', 1, 2], ['Header1', 'a1', 'b1'], ['Header2', 'a2', 'b2']]

To get the list you shared in the question, you can just add a new row to the df dataframe and then uset the commented code to get that output.

  • Related