Python 对GroupBy对象的所有成员执行操作
使用Python 对GroupBy对象的所有成员执行操作,python,python-2.7,pandas,Python,Python 2.7,Pandas,使用.groupby后,是否可以对groupby对象中的所有组作为一个整体执行操作,类似于dataframe时的行为。还是我总是需要保留一份非分组副本 test = df test_group = test.groupby(test.index.month) print test_group['A'].median() print test['A'].median() print type(test_group), type(test) 1 10.60 2 10.40 3
.groupby
后,是否可以对groupby
对象中的所有组作为一个整体执行操作,类似于dataframe
时的行为。还是我总是需要保留一份非分组副本
test = df
test_group = test.groupby(test.index.month)
print test_group['A'].median()
print test['A'].median()
print type(test_group), type(test)
1 10.60
2 10.40
3 8.70
4 14.15
5 15.80
6 13.20
7 16.15
8 20.70
9 17.90
10 15.45
11 13.25
12 11.30
Name: test, dtype: float64
14.0
<class 'pandas.core.groupby.DataFrameGroupBy'> <class 'pandas.core.frame.DataFrame'>
test=df
test\u group=test.groupby(test.index.month)
打印测试组['A'].中位数()
打印测试['A'].中位数()
打印类型(测试组)、类型(测试)
1 10.60
2 10.40
3 8.70
4 14.15
5 15.80
6 13.20
7 16.15
8 20.70
9 17.90
10 15.45
11 13.25
12 11.30
名称:test,数据类型:float64
14
您只需调用test\u group.median()
no?否,在对象上调用.median
。groupby
返回每个组的中位数(在我的例子中有12个组)。输出看起来与我的示例相同,只是它们为.groupby
对象中的每列指定了12个值。对原始数据帧调用.median()
,返回一个值(示例中的14.0)。现在我保存了和.groupby
对象的未分组副本,我想知道是否有办法避免这样做。我对这个输出感到困惑。print test_group['A'].median()
的输出不是您列出的第一个输出,而不是14.0
?抱歉,我的错误-我现在已经更正了代码。第一个输出来自分组对象,第二个输出来自数据帧。我们确定这是一个转换吗?