I have the following dataframe in Python:
my_df = pd.DataFrame([["123456", "a", "80", False, "beta", None, None], ["123456", "b", "80", False, "beta", None, None],["7891011", "a", "80", False, "beta", None, None], ["7891011", "b", "80", False, "beta", None, None]], columns = ["device", "variable", "size", "manual", "method","nrow", "ncol"])
>>> my_df.head()
device variable size manual method nrow ncol
0 123456 a 80 False beta None None
1 123456 b 80 False beta None None
2 7891011 a 80 False beta None None
3 7891011 b 80 False beta None None
I want to convert it to the following nested dict structure:
{
'123456':
{
'a': {
'size': 80,
'manual': False,
'method': 'beta',
'nrow': None,
'ncol': None
},
'b': {
'size': 80,
'manual': False,
'method': 'beta',
'nrow': None,
'ncol': None
}
},
'7891011':
{
'a': {
'size': 80,
'manual': False,
'method': 'beta',
'nrow': None,
'ncol': None
},
'b': {
'size': 80,
'manual': False,
'method': 'beta',
'nrow': None,
'ncol': None
}
}
}
I can easily loop through the variables and do some filtering with pandas, but that does not seem very efficient. Is there a way to do that using df.groupby()?
Maybe:
my_df.groupby(["device", "variable"]).apply(list).to_dict()
But that messes up the key names.