Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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 如何在for循环中检索Pandas GroupBy对象的行_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何在for循环中检索Pandas GroupBy对象的行

Python 如何在for循环中检索Pandas GroupBy对象的行,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个GROUPBY对象。我想在for循环中检索GROUPBY对象的特定列的行并进行一些处理。例如,我在这里给出了一个按对象分组的示例代码 df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B' : ['one', 'one', 'two', 'three',

我有一个GROUPBY对象。我想在for循环中检索GROUPBY对象的特定列的行并进行一些处理。例如,我在这里给出了一个按对象分组的示例代码

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                             'foo', 'bar', 'foo', 'foo'],
                       'B' : ['one', 'one', 'two', 'three',
                              'two', 'two', 'one', 'three'],
                       'C' : np.random.randn(8),
                       'D' : np.random.randn(8)})
grouped = df.groupby(['A', 'B'])
经过分组,我得到了以下对象


在for循环中,我想检查它是一个、两个还是三个,然后进行一些处理。你能给我建议一下步骤吗?

你可以按如下对象在组中循环:

for index, row in grouped:
    print (index) #index is a tuple 
    print(row) #row is a new dataframe 
('bar', 'one')
     A    B        C         D
1  bar  one -0.83026  0.983017
('bar', 'three')
     A      B         C         D
3  bar  three -0.381041  1.538971
('bar', 'two')
     A    B         C         D
5  bar  two -0.963402  0.201348
('foo', 'one')
     A    B         C         D
0  foo  one  0.691410  0.328420
6  foo  one -1.521541 -0.188345
('foo', 'three')
     A      B         C         D
7  foo  three -0.817304 -0.359331
('foo', 'two')
     A    B         C         D
2  foo  two -0.528639 -0.999301
4  foo  two -1.018919  0.661665
要检查您要查找的内容,可以执行以下操作(即:检查某个值是否在数据帧的某个列中执行此操作):

对于您的数据,输出如下:

for index, row in grouped:
    print (index) #index is a tuple 
    print(row) #row is a new dataframe 
('bar', 'one')
     A    B        C         D
1  bar  one -0.83026  0.983017
('bar', 'three')
     A      B         C         D
3  bar  three -0.381041  1.538971
('bar', 'two')
     A    B         C         D
5  bar  two -0.963402  0.201348
('foo', 'one')
     A    B         C         D
0  foo  one  0.691410  0.328420
6  foo  one -1.521541 -0.188345
('foo', 'three')
     A      B         C         D
7  foo  three -0.817304 -0.359331
('foo', 'two')
     A    B         C         D
2  foo  two -0.528639 -0.999301
4  foo  two -1.018919  0.661665

您可以按如下方式在组_中循环:

for index, row in grouped:
    print (index) #index is a tuple 
    print(row) #row is a new dataframe 
('bar', 'one')
     A    B        C         D
1  bar  one -0.83026  0.983017
('bar', 'three')
     A      B         C         D
3  bar  three -0.381041  1.538971
('bar', 'two')
     A    B         C         D
5  bar  two -0.963402  0.201348
('foo', 'one')
     A    B         C         D
0  foo  one  0.691410  0.328420
6  foo  one -1.521541 -0.188345
('foo', 'three')
     A      B         C         D
7  foo  three -0.817304 -0.359331
('foo', 'two')
     A    B         C         D
2  foo  two -0.528639 -0.999301
4  foo  two -1.018919  0.661665
要检查您要查找的内容,可以执行以下操作(即:检查某个值是否在数据帧的某个列中执行此操作):

对于您的数据,输出如下:

for index, row in grouped:
    print (index) #index is a tuple 
    print(row) #row is a new dataframe 
('bar', 'one')
     A    B        C         D
1  bar  one -0.83026  0.983017
('bar', 'three')
     A      B         C         D
3  bar  three -0.381041  1.538971
('bar', 'two')
     A    B         C         D
5  bar  two -0.963402  0.201348
('foo', 'one')
     A    B         C         D
0  foo  one  0.691410  0.328420
6  foo  one -1.521541 -0.188345
('foo', 'three')
     A      B         C         D
7  foo  three -0.817304 -0.359331
('foo', 'two')
     A    B         C         D
2  foo  two -0.528639 -0.999301
4  foo  two -1.018919  0.661665

谢谢。现在我正在检查我的groupby对象中是否有-1.5522。所以我写了一些代码,我将在这里添加我的第一条评论。我犯了一些错误。请看一看并建议meindex是元组,row是数据帧,更新您的回答是的,我认为row是数据帧。我已经更新了我的错误代码。请你看一下并给我建议好吗?是的,你的答案投了赞成票。对不起,我应该在那之前做的。我仍然有问题。我在我的评论部分加上。如果您能提供我的解决方案,那么我将接受您的回答对不起,管理员删除了我先前的修改。所以@Espoir按照你的建议我试试。成功了。但是为了测试,我使用了下面的代码
作为索引,行在分组中:如果行中有10。get(“C”):print(“hello”)
并且10不在我的列C中,但我仍然得到了hello!!谢谢。现在我正在检查我的groupby对象中是否有-1.5522。所以我写了一些代码,我将在这里添加我的第一条评论。我犯了一些错误。请看一看并建议meindex是元组,row是数据帧,更新您的回答是的,我认为row是数据帧。我已经更新了我的错误代码。请你看一下并给我建议好吗?是的,你的答案投了赞成票。对不起,我应该在那之前做的。我仍然有问题。我在我的评论部分加上。如果您能提供我的解决方案,那么我将接受您的回答对不起,管理员删除了我先前的修改。所以@Espoir按照你的建议我试试。成功了。但是为了测试,我使用了下面的代码
作为索引,行在分组中:如果行中有10。get(“C”):print(“hello”)
并且10不在我的列C中,但我仍然得到了hello!!如果你有一个新问题,那么打开一个新问题。不要修改同一件事,它对任何人都没有好处。如果你有一个新问题,那就打开一个新问题。不要修改同样的东西,它对任何人都没有好处。