Search code examples
pandascsvsumcsvreaderemptydatatext

calculate the number of rows in a column that meet certain criteria (specific word and empty rows) by using Panda


From data in a csv file, I try to obtain using Panda, the sum of the rows of a specific column ("Status_Issue") of the csv file that are:

  • empty rows and
  • rows containing the word "Pending".

Here is the code I used but unfortunately it doesn't work, can you please help me to correct my error.

def count_pending_TaxRegulatory():
df = pd.read_csv("sortdata.csv", header=1)
count1 = (df["Status_Issue"]=="Pending").sum()
count2 = df["Status_Issue"].isna().sum()
result1 = count1 + count2

Solution

  • sum() function return the sum of the values for the requested axis, so unless "Status_Issue" is only either 1 or 0 (where isna is making things 1 or 0 thus working), it won't work as you wanted. check this out Count number of rows when row contains certain text