type(Table)
pandas.core.frame.DataFrame
Table
======= ======= =======
Column1 Column2 Column3
0 23 1
1 5 2
1 2 3
1 19 5
2 56 1
2 22 2
3 2 4
3 14 5
4 59 1
5 44 1
5 1 2
5 87 3
For anyone familliar with pandas how would I build a multivalue dictionary with the .groupby()
method?
I would like an output to resemble this format:
{
0: [(23,1)]
1: [(5, 2), (2, 3), (19, 5)]
# etc...
}
where Col1
values are represented as keys and the corresponding Col2
and Col3
are tuples packed into an array for each Col1
key.
My syntax works for pooling only one column into the .groupby()
:
Table.groupby('Column1')['Column2'].apply(list).to_dict()
# Result as expected
{
0: [23],
1: [5, 2, 19],
2: [56, 22],
3: [2, 14],
4: [59],
5: [44, 1, 87]
}
However specifying multiple values for the indices results in returning column names for the value :
Table.groupby('Column1')[('Column2', 'Column3')].apply(list).to_dict()
# Result has column namespace as array value
{
0: ['Column2', 'Column3'],
1: ['Column2', 'Column3'],
2: ['Column2', 'Column3'],
3: ['Column2', 'Column3'],
4: ['Column2', 'Column3'],
5: ['Column2', 'Column3']
}
How would I return a list of tuples in the value array?