Consider, dataframe d
:
d = pd.DataFrame({'a': [0, 2, 1, 1, 1, 1, 1],
'b': [2, 1, 0, 1, 0, 0, 2],
'c': [1, 0, 2, 1, 0, 2, 2]}
> a b c
0 0 2 1
1 2 1 0
2 1 0 2
3 1 1 1
4 1 0 0
5 1 0 2
6 1 2 2
I want to split it by column a
into dictionary like that:
{0: a b c
0 0 2 1,
1: a b c
2 1 0 2
3 1 1 1
4 1 0 0
5 1 0 2
6 1 2 2,
2: a b c
1 2 1 0}
The solution I've found using pandas.groupby
is:
{k: table for k, table in d.groupby("a")}
What are the other solutions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…