如何在Python中执行 COUNTIF函数?
在本文中,我们将讨论如何在Python中执行 COUNTIF函数。
计数
如果满足条件,我们使用此函数对元素进行计数。请注意,该词表示为COUNT + IF。这意味着如果满足所提供的条件,我们想要计算元素。
方法
- 我们将有一个包含一些列的 DataFrame。
- 我们将使用函数sum()。 sum()函数将采用Iterable值。我们将有一个数据框,其中的列包含元素列表。然后我们将通过条件来检查当前元素是否满足。
- sum()返回一个整数值。所以我们将存储该值并打印它。
句法
sum()函数的语法如下。
sum(data-list condition)
让我们举个例子,我们有一个名为 myList 的列表,在列表中,有整数值。我们希望项目数大于等于 40。所以我们可以使用 sum函数如下,
sum(mylist >= 40)
对于使用两个条件,我们可以使用 AND( & ) 或 OR( | ) 来分隔这两个条件。
sum((myList) >= 40 & (myList <= 90)) # AND
sum((myList) >= 40 | (myList <= 90)) # OR
方法一:使用单列
首先,让我们创建一个DataFrame 。这里我们有两列,分别是views和likes 。我们将保持每列的长度相同。
Python3
# create a dictionary
my_data = {"views": [12, 13, 100, 80, 91],
"likes": [3, 8, 23, 17, 56]}
# convert to dataframe
my_df = pd.DataFrame(my_data)
Python3
import pandas as pd
# Data
my_data = {"views": [12, 13, 100, 80, 91],
"likes": [3, 8, 23, 17, 56]}
my_df = pd.DataFrame(my_data)
# Printing the DataFrame
print(my_df.to_string())
# Printing the number of views greater
# than 30
print("View greater than 30: ",
sum(my_df.views > 30))
Python3
import pandas as pd
# Data
my_data = {"views": [12, 13, 100, 80, 91],
"likes": [3, 8, 23, 17, 56]}
my_df = pd.DataFrame(my_data)
# Printing the DataFrame
print(my_df.to_string())
# Printing the number of likes greater
# than 20
print("Likes greater than 20: ",
sum(my_df.likes > 20))
Python3
import pandas as pd
# Data
my_data = {"views": [12, 13, 100, 80, 91], "likes": [3, 8, 23, 17, 56]}
my_df = pd.DataFrame(my_data) # DataFrame
# Printing the DataFrame
print(my_df.to_string())
# Calculating the number of views greater than 30
# as well as likes less than 20
sum = sum((my_df.likes < 20) & (my_df.views > 30))
print("Likes less than 20 and Views more than 30: ", sum)
Python3
import pandas as pd
# Data
my_data = {"views": [12, 13, 100, 80, 91], "likes": [3, 8, 23, 17, 56]}
my_df = pd.DataFrame(my_data) # DataFrame
# Printing the DataFrame
print(my_df.to_string())
# Calculating the number of views greater than 30
# or likes less than 20
sum = sum((my_df.likes < 20) | (my_df.views > 30))
print("Likes less than 20 or Views more than 30: ", sum)
条件一:如果浏览量超过30
我们将使用sum()函数检查视图列表列中的值是否大于30 。然后 sum函数将计算对应视图大于30的行。
Python3
import pandas as pd
# Data
my_data = {"views": [12, 13, 100, 80, 91],
"likes": [3, 8, 23, 17, 56]}
my_df = pd.DataFrame(my_data)
# Printing the DataFrame
print(my_df.to_string())
# Printing the number of views greater
# than 30
print("View greater than 30: ",
sum(my_df.views > 30))
输出
条件2:如果点赞数超过20
sum()函数检查喜欢列列表中的值是否大于20 。然后 sum函数将计算相应喜欢大于 20 的行。
Python3
import pandas as pd
# Data
my_data = {"views": [12, 13, 100, 80, 91],
"likes": [3, 8, 23, 17, 56]}
my_df = pd.DataFrame(my_data)
# Printing the DataFrame
print(my_df.to_string())
# Printing the number of likes greater
# than 20
print("Likes greater than 20: ",
sum(my_df.likes > 20))
输出
方法2:使用多列
条件 1:点赞数少于 20 且查看数超过 30。
要满足两个或多个条件,请将每个条件括在括号()中,然后使用单 & 符号将它们分开。这里我们只有两个条件,所以我们只需要一个 & 。
Python3
import pandas as pd
# Data
my_data = {"views": [12, 13, 100, 80, 91], "likes": [3, 8, 23, 17, 56]}
my_df = pd.DataFrame(my_data) # DataFrame
# Printing the DataFrame
print(my_df.to_string())
# Calculating the number of views greater than 30
# as well as likes less than 20
sum = sum((my_df.likes < 20) & (my_df.views > 30))
print("Likes less than 20 and Views more than 30: ", sum)
输出
条件 2:使用 OR 条件
我们将使用单个 |签署以分隔条件。 |用作第一个条件或第二个或第三个等等。
Python3
import pandas as pd
# Data
my_data = {"views": [12, 13, 100, 80, 91], "likes": [3, 8, 23, 17, 56]}
my_df = pd.DataFrame(my_data) # DataFrame
# Printing the DataFrame
print(my_df.to_string())
# Calculating the number of views greater than 30
# or likes less than 20
sum = sum((my_df.likes < 20) | (my_df.views > 30))
print("Likes less than 20 or Views more than 30: ", sum)
输出