How to use a single column In Python
First, let us create a DataFrame. Here we have two columns, which are views and likes. We will keep the length of each column the same.
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) |
Condition 1: If the views are more than 30
We will use the sum() function to check if, in the list of views column, the values are greater than 30. Then the sum function will count the rows that have corresponding views greater than 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 )) |
Output
Condition 2: If the likes are more than 20
The sum() function to check if, in the list of likes column, the values are greater than 20. Then the sum function will count the rows that have corresponding likes greater than 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 )) |
Output
How to Perform a COUNTIF Function in Python?
In this article, we will discuss how to perform a COUNTIF function in Python.
Contact Us