pandas groupby删除列 [英] pandas groupby dropping columns
问题描述
我正在按操作进行简单分组,试图比较分组均值.如下所示,我从一个较大的数据框中选择了特定的列,从中删除了所有缺失的值.
I'm doing a simple group by operation, trying to compare group means. As you can see below, I have selected specific columns from a larger dataframe, from which all missing values have been removed.
但是当我分组时,我丢失了几列:
But when I group by, I am losing a couple of columns:
我从来没有遇到过熊猫,而且在堆栈溢出中我找不到其他类似的东西.有人有见识吗?
I have never encountered this with pandas, and I'm not finding anything else on stack overflow that is all that similar. Does anybody have any insight?
推荐答案
我认为它是Automatic exclusion of 'nuisance' columns
,它描述了
I think it is Automatic exclusion of 'nuisance' columns
, what described here.
示例:
df = pd.DataFrame({'C': {0: -0.91985400000000006, 1: -0.042379, 2: 1.2476419999999999, 3: -0.00992, 4: 0.290213, 5: 0.49576700000000001, 6: 0.36294899999999997, 7: 1.548106}, 'A': {0: 'foo', 1: 'bar', 2: 'foo', 3: 'bar', 4: 'foo', 5: 'bar', 6: 'foo', 7: 'foo'}, 'B': {0: 'one', 1: 'one', 2: 'two', 3: 'three', 4: 'two', 5: 'two', 6: 'one', 7: 'three'}, 'D': {0: -1.131345, 1: -0.089328999999999992, 2: 0.33786300000000002, 3: -0.94586700000000001, 4: -0.93213199999999996, 5: 1.9560299999999999, 6: 0.017587000000000002, 7: -0.016691999999999999}})
print (df)
A B C D
0 foo one -0.919854 -1.131345
1 bar one -0.042379 -0.089329
2 foo two 1.247642 0.337863
3 bar three -0.009920 -0.945867
4 foo two 0.290213 -0.932132
5 bar two 0.495767 1.956030
6 foo one 0.362949 0.017587
7 foo three 1.548106 -0.016692
print( df.groupby('A').mean())
C D
A
bar 0.147823 0.306945
foo 0.505811 -0.344944
我认为您可以检查 DataFrame.dtypes
.
I think you can check DataFrame.dtypes
.
这篇关于 pandas groupby删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!