在 pandas 文档 ( http://pandas.pydata.org/pandas-docs/stable/groupby.html ) 中,使用 groupby 和下面的 get_letter_type 函数的示例。为什么描述的结果不包括“B”列?
In [5]: def get_letter_type(letter):
...: if letter.lower() in 'aeiou':
...: return 'vowel'
...: else:
...: return 'consonant'
...:
In [6]: grouped = df.groupby(get_letter_type, axis=1)
In [7]: grouped.describe()
结果显示here ,其中没有 B 列。谁能解释为什么?因为 B 在我看来应该属于 'consnant' 组,所以我错过了什么吗?
请您参考如下方法:
对我来说,如果 DataFrame 只有 A 和 B 列,它就可以工作:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three']})
def get_letter_type(letter):
if letter.lower() in 'aeiou':
return 'vowel'
else:
return 'consonant'
grouped = df.groupby(get_letter_type, axis=1)
for i, g in (grouped):
print (i)
print (g)
consonant
B
0 one
1 one
2 two
3 three
4 two
5 two
6 one
7 three
vowel
A
0 foo
1 bar
2 foo
3 bar
4 foo
5 bar
6 foo
7 foo
print (grouped.describe())
consonant vowel
B A
count 8 8
unique 3 2
top one foo
freq 3 5
我认为有 automatic exclusion of nuisance columns ,如果一些组,例如consonant 包含 numeric 和 string 列:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
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)})
def get_letter_type(letter):
if letter.lower() in 'aeiou':
return 'vowel'
else:
return 'consonant'
grouped = df.groupby(get_letter_type, axis=1)
for i, g in (grouped):
print (i)
print (g)
consonant
B C D
0 one 0.322759 0.348806
1 one -0.122110 -1.566801
2 two 1.846408 -0.830144
3 three -0.509248 0.486773
4 two -1.061608 -0.069366
5 two 1.083728 0.429543
6 one -0.664480 -0.702906
7 three 0.587159 0.978647
vowel
A
0 foo
1 bar
2 foo
3 bar
4 foo
5 bar
6 foo
7 foo
print (grouped.describe())
consonant vowel
C D A
25% -0.548056 -0.734716 NaN
50% 0.100325 0.139720 NaN
75% 0.711301 0.443851 NaN
count 8.000000 8.000000 8
freq NaN NaN 5
max 1.846408 0.978647 NaN
mean 0.185326 -0.115681 NaN
min -1.061608 -1.566801 NaN
std 0.971055 0.848251 NaN
top NaN NaN foo
unique NaN NaN 2
