📜  在Python Pandas 中执行类似计数的 Excel

📅  最后修改于: 2022-05-13 01:54:34.419000             🧑  作者: Mango

在Python Pandas 中执行类似计数的 Excel

在本文中,我们将在 Pandas 中执行类似 excel 的计数。在 Excel 中,数据是表格的形式,因此我们可以通过在指定列上指定标准来执行许多算术运算,例如值的总和、平均值和行数等。同样,我们可以在Python中对 Pandas DataFrame 执行所有这些操作。由于 DataFrame 还以表格格式维护数据。

计数

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

示例 1:在 Pandas 中执行类似 excel 的计数

Python3
# 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)


Python3
# 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)


Python3
# 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)


输出:

Number of Twills Shirts-2

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

示例 2:这里我们也使用与上述相同的 DataFrame,但不是查找 Twills 品牌衬衫的计数,而是查找任何品牌的 Count of Shirts。

Python3

# 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)

输出:

示例 3:使用上述 Costume DataFrame 查找价格小于或等于 2000 的牛仔裤的数量

Python3

# 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)

输出: