在Python Pandas中执行类似Excel的counttifs操作

在Python Pandas中执行类似Excel的counttifs操作

在这篇文章中,我们将在Pandas中进行类似于Excel的计数运算。在Excel中,数据是以表的形式存在的,所以我们可以通过在指定的列上指定标准来执行许多算术运算,如值的总和,行的平均数和计数,等等。同样地,我们可以在Python中对Pandas DataFrame进行所有这些操作。因为DataFrame也是以表格式保存数据的。

Countifs

它是一种通过指定一个或多个条件(与在线购物应用中的过滤器相同)来查找行数以获得所需结果的操作。还有一些类似count()的方法,如sum()、mean()等,用于查找相应的数据之和和数据的平均值。

例子1:在Pandas中执行类似excel的counttifs。

# import necessary packages
import pandas as pd
  
# create a dataframe
costumes = pd.DataFrame({'Brand': ['Twills', 'Wrogn',
                                   'Twills', 'Trigger',
                                   'Twills', 'Wrogn', ],
                         'Costume_Type': ['Shirt', 'Shirt',
                                          'Shirt', 'Jeans',
                                          'T-Shirt', 'Jeans'],
                         'price': [1699, 1999, 1569,
                                   2000, 569, 2400]})
  
# DataFrame
print(costumes)
  
# find count of Twills Shirts
twills_Shirt_Count = costumes.query('Brand=="Twills" \
& Costume_Type=="Shirt"')['Costume_Type'].count()
  
print('Number of Twills Shirts-', end="")
print(twills_Shirt_Count)

输出:

Number of Twills Shirts-2

在Python Pandas中执行类似Excel的counttifs操作

解释:因为我们有3个Twills品牌的项目,但在这3个项目中,我们有2条记录,其中服装类型为衬衫,所以它返回了2个结果。

示例2:这里我们也使用上述相同的数据框架,但不是寻找Twills品牌衬衫的数量,而是寻找任何品牌的衬衫的数量。

# import necessary packages
import pandas as pd
  
# create a dataframe
costumes = pd.DataFrame({'Brand': ['Twills', 'Wrogn', 
                                   'Twills', 'Trigger',
                                   'Twills', 'Wrogn', ],
                         'Costume_Type': ['Shirt', 'Shirt',
                                          'Shirt', 'Jeans', 
                                          'T-Shirt', 'Jeans'],
                         'price': [1699, 1999, 1569, 
                                   2000, 569, 2400]})
  
# DataFrame
print(costumes)
  
# find count of Twills Shirts
Shirt_Count = costumes.query('Costume_Type=="Shirt"')
['Costume_Type'].count()
  
print('\nNumber of Shirts-', end="")
print(Shirt_Count)

输出:

在Python Pandas中执行类似Excel的counttifs操作

例子3:使用上述服装数据框架,找到价格小于或等于2000的牛仔裤的数量。

# import necessary packages
import pandas as pd
  
# create a dataframe
costumes = pd.DataFrame({'Brand': ['Twills', 'Wrogn',
                                   'Twills', 'Trigger', 
                                   'Twills', 'Wrogn', ],
                         'Costume_Type': ['Shirt', 'Shirt', 
                                          'Shirt', 'Jeans', 
                                          'T-Shirt', 'Jeans'],
                         'price': [1699, 1999, 1569,
                                   2000, 569, 2400]})
  
# DataFrame
print(costumes)
  
# find count of Twills Shirts
Jeans_Count = costumes.query('Costume_Type=="Jeans" & price<=2000')[
    'Costume_Type'].count()
  
print('\nNumber of Jeans below or equals to Rs.2000-', end=" ")
print(Jeans_Count)

输出:

在Python Pandas中执行类似Excel的counttifs操作

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程