-1

I have below csv file. And i am reading this csv file using pandas dataframe.

Input:

enter image description here

Now i am trying to fetch if 2 columns city != null and state != null and country == null then it will return those row only.

Output

enter image description here

Code

def fieldValidation(df):
    try:
        nan_rows = df[df['city'].notnull(), df['state'].notnull(), df['country'].isnull()]
        print(nan_rows)
    except Exception as error:
        print(error)
    

Can anyone tell me where i am doing wrong or how can i achive this?

Sangram Badi
  • 4,054
  • 9
  • 45
  • 78
  • 1
    Does this answer your question? [Selecting with complex criteria from pandas.DataFrame](https://stackoverflow.com/questions/15315452/selecting-with-complex-criteria-from-pandas-dataframe) – MrNobody33 Aug 21 '20 at 09:30

1 Answers1

1

Use & for bitwise AND instead , for filtering by multiple conditions:

df1 = df[df['city'].notnull() & df['state'].notnull() & df['country'].isnull()]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252