python - GroupBy results to dictionary of lists -
i have excel sheet looks so:
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
and i'm looking extract data, group column 1, , add dictionary appears this:
{0: [1], 1: [2,3,5], 2: [1,2], 3: [4,5], 4: [1], 5: [1,2,3]}
this code far
excel = pandas.read_excel(r"e:\test_data.xlsx", sheetname='mysheet', parse_cols'a,c') mytable = excel.groupby("column1").groups print mytable
however, output looks this:
{0: [0l], 1: [1l, 2l, 3l], 2: [4l, 5l], 3: [6l, 7l], 4: [8l], 5: [9l, 10l, 11l]}
thanks!
you groupby
on column1
, take column3
apply(list)
, call to_dict
?
in [81]: df.groupby('column1')['column3'].apply(list).to_dict() out[81]: {0: [1], 1: [2, 3, 5], 2: [1, 2], 3: [4, 5], 4: [1], 5: [1, 2, 3]}
Comments
Post a Comment