user3277468
user3277468

Reputation: 141

Pandas checking if a column contains a 0 and another is not null

I have a data set I am reading with csv file and I want to grab the row number/index where an if statement is true.

So if the column row value is 0 and another column value in the same row is not null.

Right now my loop is showing that all rows in my data set have a 0 and not null which is wrong, so its not working.

What am I doing wrong?

counter = 0

for index, row in raw_csv_data.iterrows():
    if(row['column1'] == 0 and row['column3'] != np.nan):
        print(row['column1'], row['column3'])

Solution fixed part of if statement

row.isna()['column3'] == False

Upvotes: 0

Views: 1100

Answers (2)

seralouk
seralouk

Reputation: 33147

Another way to do this is the following:

counts = sum((data['column1'].eq(0) & ~data['column3'].isna()))

eq is a method to check if the values are equal to 0 (see here)

Similarity, for isna() see here

Upvotes: 1

abhikumar22
abhikumar22

Reputation: 1814

Problem is this statement have syntactical issue, please check the below line of code and replace it with yous and then it will works for sure

if(row['column1'] == 0 and row['column3'] != np.nan)

below is your complete code

counter = 0

for index, row in raw_csv_data.iterrows():
    if(row['column1'] == 0 and row['column3'] != np.nan)
        counter += 1

Upvotes: 0

Related Questions