Home > Back-end >  Pandas looping through rows check if one column row is empty and another is not
Pandas looping through rows check if one column row is empty and another is not

Time:02-15

I have the below output that I place into a df. Now I want to loop through each row and check if the comment col. is not empty and if status col is empty. The 'yes' print out once because the df only has 1 that meets these requirements but it print out twice:

output = [['table_name', 'schema_name', 'column_name', 'data_type', 'null?', 'default', 'kind', 'expression', 'comment', 'database_name', 'autoincrement', 'Status'], ['ACCOUNT', 'SO', '_LOAD_DATETIME', '{"type":"TIMESTAMP_LTZ","precision":0,"scale":9,"nullable":true}', 'TRUE', '', 'COLUMN', '', 'date and time when table was loaded', 'DEV'], ['ACCOUNT', 'SO', '_LOAD_FILENAME', '{"type":"TEXT","length":16777216,"byteLength":16777216,"nullable":true,"fixed":false}', 'TRUE', '', 'COLUMN', '', '', 'DEV']]

output = filter(bool, output)


df = pd.DataFrame(output)

df.columns = df.iloc[0]
df = df[1:]


query_list = []


for index, row in df.iterrows():
    if row['Status'] is None and row['comment'] is not None or row['comment'] != '':
        print('yes')

CodePudding user response:

Your condition starts as

if row['Status'] is None and row['comment'] is not None...

and if you look at "Status" column, it is:

1    None
2    None
Name: Status, dtype: object

so the if-condition is True twice.

CodePudding user response:

Your if statement is incorrect. You should have

if row['Status'] is None and row['comment'] is not None and row['comment'] != '':
  • Related