Python create parse a dictionary to another format
Question:
I wanted to parse a dictionary with a format. Currently this dictionary has the same user ID in separate dictionaries and I wanted to make it same user ID in one dictionary with key and values.
Below is my current dictionary:
{
"key1": {
"userID1": 5,
"userID2": 1,
"userID3": 5
},
"key2": {
"userID1": 13,
"userID2": 24,
"userID3": 0
},
"key3": {
"userID1": 14,
"userID2": 12,
"userID3": 3
}
}
Expected dictionary:
{
"userID1": {
"key1": 5,
"key2": 13,
"key3": 14
},
"userID2": {
"key1": 1,
"key2": 24,
"key3": 3
},
.....
}
Answers:
Here is one possible example how you can transform the dictionary:
original_dictionary = {
"key1": {"userID1": 5, "userID2": 1, "userID3": 5},
"key2": {"userID1": 13, "userID2": 24, "userID3": 0},
"key3": {"userID1": 14, "userID2": 12, "userID3": 3},
}
parsed_dictionary = {}
for k, v in original_dictionary.items():
for kk, vv in v.items():
parsed_dictionary.setdefault(kk, {})[k] = vv
print(parsed_dictionary)
Prints:
{
"userID1": {"key1": 5, "key2": 13, "key3": 14},
"userID2": {"key1": 1, "key2": 24, "key3": 12},
"userID3": {"key1": 5, "key2": 0, "key3": 3},
}
Another option is available if you use Pandas:
#!/usr/bin/env python
import pandas as pd
in = {
"key1": {
"userID1": 5,
"userID2": 1,
"userID3": 5
},
"key2": {
"userID1": 13,
"userID2": 24,
"userID3": 0
},
"key3": {
"userID1": 14,
"userID2": 12,
"userID3": 3
}
}
df = pd.DataFrame.from_dict(in)
out = df.T.to_dict()
print(out)
Prints:
{'userID1': {'key1': 5, 'key2': 13, 'key3': 14}, 'userID2': {'key1': 1, 'key2': 24, 'key3': 12}, 'userID3': {'key1': 5, 'key2': 0, 'key3': 3}}
This reads your dictionary into a Pandas dataframe, takes the transpose, and converts the transposed dataframe back to a Python dictionary.
I wanted to parse a dictionary with a format. Currently this dictionary has the same user ID in separate dictionaries and I wanted to make it same user ID in one dictionary with key and values.
Below is my current dictionary:
{
"key1": {
"userID1": 5,
"userID2": 1,
"userID3": 5
},
"key2": {
"userID1": 13,
"userID2": 24,
"userID3": 0
},
"key3": {
"userID1": 14,
"userID2": 12,
"userID3": 3
}
}
Expected dictionary:
{
"userID1": {
"key1": 5,
"key2": 13,
"key3": 14
},
"userID2": {
"key1": 1,
"key2": 24,
"key3": 3
},
.....
}
Here is one possible example how you can transform the dictionary:
original_dictionary = {
"key1": {"userID1": 5, "userID2": 1, "userID3": 5},
"key2": {"userID1": 13, "userID2": 24, "userID3": 0},
"key3": {"userID1": 14, "userID2": 12, "userID3": 3},
}
parsed_dictionary = {}
for k, v in original_dictionary.items():
for kk, vv in v.items():
parsed_dictionary.setdefault(kk, {})[k] = vv
print(parsed_dictionary)
Prints:
{
"userID1": {"key1": 5, "key2": 13, "key3": 14},
"userID2": {"key1": 1, "key2": 24, "key3": 12},
"userID3": {"key1": 5, "key2": 0, "key3": 3},
}
Another option is available if you use Pandas:
#!/usr/bin/env python
import pandas as pd
in = {
"key1": {
"userID1": 5,
"userID2": 1,
"userID3": 5
},
"key2": {
"userID1": 13,
"userID2": 24,
"userID3": 0
},
"key3": {
"userID1": 14,
"userID2": 12,
"userID3": 3
}
}
df = pd.DataFrame.from_dict(in)
out = df.T.to_dict()
print(out)
Prints:
{'userID1': {'key1': 5, 'key2': 13, 'key3': 14}, 'userID2': {'key1': 1, 'key2': 24, 'key3': 12}, 'userID3': {'key1': 5, 'key2': 0, 'key3': 3}}
This reads your dictionary into a Pandas dataframe, takes the transpose, and converts the transposed dataframe back to a Python dictionary.