2016-12-21 36 views
-1

我在熊貓有一個頻率表。我想將每種氣味等級的頻率提取到一個列表中,每個元素都是一個NumPy數組,其中包含給定氣味等級的頻率數。我怎樣才能做到這一點,而無需手動指定行和列?如何從熊貓頻率表中提取數組

這是我有:

test=df.groupby(['odor', 'class']) 
test.size() 

導致:

odor class 
a  e   400 
c  p   192 
f  p  2160 
l  e   400 
m  p   36 
n  e  3408 
     p   120 
p  p   256 
s  p   576 
y  p   576 
dtype: int64 

回答

0

請您提供的零個數據一起工作的想法。根據你留下的一半線索,我完全猜測你的數據是什麼樣的。

df = pd.DataFrame(
    { 
     'odor': np.random.choice(list('acflmnpsy'), 100), 
     'class': np.random.choice(list('ep'), 100), 
     'frequency': np.random.choice(np.arange(60, 241), 100) 
    } 
) 

df.groupby(['odor', 'class']).frequency.apply(np.array) 

odor class 
a  e        [170, 178, 67, 97] 
     p         [183, 165, 73] 
c  e       [106, 163, 71, 171] 
     p      [72, 224, 112, 196, 134] 
f  e    [134, 67, 190, 226, 74, 62, 107] 
     p    [222, 74, 177, 186, 122, 120, 121] 
l  e     [81, 97, 124, 181, 101, 185] 
     p       [182, 136, 77, 190] 
m  e     [217, 121, 141, 102, 171, 106] 
     p    [127, 178, 127, 72, 209, 63, 167] 
n  e     [166, 121, 79, 117, 130, 92] 
     p       [71, 136, 219, 155] 
p  e   [118, 193, 147, 219, 154, 130, 169] 
     p    [69, 162, 138, 193, 236, 144, 192] 
s  e        [105, 139, 143] 
     p      [72, 149, 186, 220, 63] 
y  e   [143, 134, 102, 144, 186, 207, 181] 
     p  [177, 215, 190, 158, 203, 157, 85, 172] 
Name: frequency, dtype: object 
0

這是您要尋找的嗎?

df.pivot_table(index = ['Class'],columns = ['Odor'],value = ['freq'],aggfunc ='sum')