簡體   English   中英

根據標准對事件進行計數

[英]Counting occurrences based on criterion

我試圖計算一種顏色在一個數據框中出現的總次數,但是我只希望它根據選定的條件選擇它們。 例如我有:

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')...

我嘗試過count(),但是它顯示了每列中列出的每種形狀的每次總計數。 在Pandas中是否有類似於SQL“ where”的東西? 我認為我可能需要對聚合進行某些操作,但是到目前為止,我一直沒有成功使用它。

編輯:這是我得到的byShape = df.groupby('shape')。count()

                      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  

我會用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

我的看法是使用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
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM