1

I'm trying to create new columns in a dateframe that will indicate blank or not blank for the rest of the columns. I think this should be fairly simple, but I'm having trouble getting the code quite right...

for column, row in df.iterrows():
if(pd.isnull(row[column])):
    df[column + 'Blank or Not'] = "blank"
else:
    df[column + 'Blank or Not'] = "not blank"

This is the error:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

When I changed it to below:

for column, row in df.iterrows():
    if(pd.isnull(row[column])):
        df[str(column) + 'Blank or Not'] = "blank"
    else:
        df[str(column) + 'Blank or Not'] = "not blank"

This is the error:

 IndexError: index out of bounds
mk2080
  • 872
  • 1
  • 8
  • 21

1 Answers1

0

I suspect the column names are int, so use:

if(pd.isnull(row[column])):
    df[str(column) + 'Blank or Not'] = "blank"
else:
    df[str(column) + 'Blank or Not'] = "not blank"

or in python 3 you can use f-strings:

f"{column}Blank or Not"

Edit: I think you're wanting to do something slightly different:

In [21]: df
Out[21]:
    0  1
0 NaN  1

In [22]: df.isnull().applymap({True: 'Blank', False: 'Not Blank'}.get)
Out[22]:
       0          1
0  Blank  Not Blank
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535