Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用groupby将每个组转换为数据帧_Python 3.x_Pandas_Dataframe_Group By - Fatal编程技术网

Python 3.x 如何使用groupby将每个组转换为数据帧

Python 3.x 如何使用groupby将每个组转换为数据帧,python-3.x,pandas,dataframe,group-by,Python 3.x,Pandas,Dataframe,Group By,我有一个数据框看起来像 A B 1 2 1 3 1 4 2 5 2 6 3 7 3 8 如果我df.groupby('A'),我如何将每个组转换成子数据帧,因此它看起来像A=1 A B 1 2 1 3 1 4 对于A=2 A B 2 5 2 6 对于A=3 A B 3 7 3 8 您已关闭,需要将groupby对象转换为数据帧的字典s: dfs = dict(tuple(df.g

我有一个数据框看起来像

A    B
1    2
1    3
1    4
2    5
2    6
3    7
3    8
如果我
df.groupby('A')
,我如何将每个组转换成子数据帧,因此它看起来像
A=1

A    B
1    2
1    3
1    4
对于
A=2

A    B
2    5
2    6
对于
A=3

A    B
3    7
3    8 

您已关闭,需要将
groupby
对象转换为数据帧的
字典
s:

dfs = dict(tuple(df.groupby('A')))
print (dfs[1])
   A  B
0  1  2
1  1  3
2  1  4

print (dfs[2])
   A  B
3  2  5
4  2  6

使用
get\u组

g=df.groupby('A')
g.get_group(1)
Out[367]: 
   A  B
0  1  2
1  1  3
2  1  4