如何从“单元"获取值. “分组依据"目的? [英] How to get values from a "cell" of a "groupby" object?
本文介绍了如何从“单元"获取值. “分组依据"目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我具有以下数据框:
Assume that I have the following data frame:
A B C D
0 foo one 1 10
1 bar one 2 20
2 foo two 3 30
3 bar one 4 40
4 foo two 5 50
5 bar two 6 60
6 foo one 7 70
7 foo two 8 80
现在我可以按第一列进行分组:grouped = df.groupby('A')
.结果,我得到以下DataFrameGroupBy
对象:
Now I can group by the first column: grouped = df.groupby('A')
. As a result I get the following DataFrameGroupBy
object:
A B C D
0 foo [one,two,two,one,two] [1,3,5,7,8] [10,30,50,70,80]
1 bar [one,one,two] [2,4,6] [20,40,60]
现在,我想从特定单元格访问值.我该怎么做?例如,我想从列'D'和'A'=='foo'
行(第一行)中获取值.换句话说,我想获取[10,30,50,70,80]
.有可能吗?
Now I would like to access the values from a particular cell. How can I do it? For example I want to get the values from the column 'D' and the row where 'A'=='foo'
(the first row). In other words I want to get [10,30,50,70,80]
. Is it possible?
推荐答案
您是否正在考虑这样的事情?
Are you thinking of something like this?
>>> df
A B C D
0 foo one 1 10
1 bar one 2 20
2 foo two 3 30
3 bar one 4 40
4 foo two 5 50
5 bar two 6 60
6 foo one 7 70
7 foo two 8 80
>>> df.groupby("A").get_group("foo")["D"]
0 10
2 30
4 50
6 70
7 80
Name: D
>>> df.groupby("A").get_group("foo")["D"].tolist()
[10, 30, 50, 70, 80]
这篇关于如何从“单元"获取值. “分组依据"目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文