2015-03-02 100 views
0

我正在嘗試計算某個數據框中顯示的顏色的總次數,但我只希望它在選定的條件上選擇它們。 比如我有:根據標準計算出現次數

imageName  color1  color2  color3  color4  shape 
img1   Red  Red  Red  Red  circle 
img2   Blue  Green  Red  Blue  circle 
img3   Yellow  Blue  Red  White  square 
img4   Blue  Blue  Blue  Blue  circle 

我要選擇「紅色」的所有出現在那裏形狀==圈。 我已經試過GROUPBY但我的概念是什麼,我應該有一些麻煩,在做:

byShape = df.groupby('shape')... 

我試過數(),但它顯示了每個時間每個形狀的總數列在每一列中。有沒有類似於Pandas中的SQL'where'的地方?我認爲我可能需要用匯總來做一些事情,但迄今爲止我還沒有成功使用它。

編輯:這是我得到byShape = df.groupby(「形狀」)計數()

     imageName color1 color2 color3 color4 
shape              
cirle     3   3  3  3  3 
square     1   1  1  1  1 

編輯編輯:我希望得到一個最終的輸出是這樣的:

Circle: Red  5 
     Blue 6 
     Green 1 
Square: Yellow 1 
     Blue 1 
     Red  1 
     White 1 
+1

你的例子應該輸出什麼? – 2015-03-02 19:27:53

+0

我將它添加到主文章 – Johnsonge 2015-03-02 19:33:03

+1

您已添加您指示不起作用的命令的輸出,是不是?如果我正確地理解了你,你想要的是,對於每一列,出現「紅色」的次數,但是隻有當該行的形狀列也是「圓」時。是對的嗎? – 2015-03-02 19:39:07

回答

4

我會用melt打開幀,然後size

>>> melted = pd.melt(df, id_vars=["imageName", "shape"], value_name="color") 
>>> melted.groupby(["shape","color"]).size() 
shape color 
circle Blue  6 
     Green  1 
     Red  5 
square Blue  1 
     Red  1 
     White  1 
     Yellow 1 
dtype: int64 

如果你想要一個框架出來的,而不是一個系列,那是很容易得:

>>> melted.groupby(["shape","color"]).size().reset_index(name="count") 
    shape color count 
0 circle Blue  6 
1 circle Green  1 
2 circle  Red  5 
3 square Blue  1 
4 square  Red  1 
5 square White  1 
6 square Yellow  1 
+0

謝謝!我從來沒有使用過熔體,但它完美的工作! – Johnsonge 2015-03-03 02:53:50

+0

另外,快速跟進問題。我將如何去除總數中的每一個值?就像說總共有7個藍調。我想圈藍色顯示爲86%,然後方形藍色爲14%。我有總計,但我不知道如何將它合併到您的解決方案 – Johnsonge 2015-03-03 03:35:08

+0

@Johnsonge:SO以問題/答案格式工作,而不是線程問題,因此非常不鼓勵後續問題。這就是說,像「大小」=融化了。GROUPBY([ 「形狀」, 「顏色」])尺寸();尺寸/ sizes.groupby(level =「color」)。sum()'應該工作。 PS:我注意到你還沒有接受任何問題的答案,所以你可能想閱讀[this](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer -工作)。 – DSM 2015-03-03 03:40:06

1
import pandas as pd 

df = pd.DataFrame({'imageName':['img1','img2','img3','img4'],      
       'color1':['Red','Blue','Yellow','Blue'], 
       'color2':['Red','Green','Blue','Blue'], 
       'color3':['Red','Red','Red','Blue'], 
       'color4':['Red','Blue','White','Blue'], 
       'shape':['circle','circle','square','circle']}) 

df.set_index('imageName',inplace=True) 

test = df.set_index('shape').stack() 
df1 = pd.DataFrame(test.values,test.index.droplevel(1)) 
df1.columns = ['Color'] 
df1['value'] = 1 
df1.groupby([df1.index,'Color']).sum() 

輸出:

   value 
shape Color   
circle Blue  6 
     Green  1 
     Red   5 
square Blue  1 
     Red   1 
     White  1 
     Yellow  1 
+0

img4不符合條件,全是藍色。 – cphlewis 2015-03-02 20:04:48

+0

當我這樣做時,如果我通過驗證計數,則會遇到計數問題,因爲顏色和形狀標記爲真。有沒有解決的辦法?我還在原始帖子中爲我的最終目標拍攝了樣片,看起來像 – Johnsonge 2015-03-03 02:08:53

+0

@Johnsonge已更新。 – 2015-03-03 02:37:41

2

使用meltpivot_table我服食。

import pandas as pd 

df = pd.DataFrame({'color1': {0: 'Red', 1: 'Blue', 2: 'Yellow', 3: 'Blue'}, 'color2': {0: 'Red', 1: 'Green', 2: 'Blue', 3: 'Blue'}, 'color3': {0: 'Red', 1: 'Red', 2: 'Red', 3: 'Blue'}, 'color4': {0: 'Red', 1: 'Blue', 2: 'White', 3: 'Blue'}, 'shape': {0: 'circle', 1: 'circle', 2: ' square', 3: 'circle'}, 'imageName': {0: 'img1', 1: 'img2', 2: 'img3', 3: 'img4'}}) 
df = df[['shape','color1','color2','color3','color4']] 
cheese = pd.melt(df, id_vars=['shape'], value_vars=['color1','color2','color3','color4']) 
pvt = pd.pivot_table(cheese, index=['shape', 'value'], aggfunc=len) 

print pvt 

結果:

   variable 
shape value   
square Blue   1 
     Red   1 
     White   1 
     Yellow   1 
circle Blue   6 
     Green   1 
     Red   5 

這是樞轉之前cheese

 shape variable value 
0 circle color1  Red 
1 circle color1 Blue 
2 square color1 Yellow 
3 circle color1 Blue 
4 circle color2  Red 
5 circle color2 Green 
6 square color2 Blue 
7 circle color2 Blue 
8 circle color3  Red 
9 circle color3  Red 
10 square color3  Red 
11 circle color3 Blue 
12 circle color4  Red 
13 circle color4 Blue 
14 square color4 White 
15 circle color4 Blue