0

I am trying to create a new column to a dataframe called names where the values = Y if the row value under 'name' = '-1A'. My dataframe is dynamic though so sometimes depending on what period i pull the data for this dataframe, my dataframe will be EMPTY (all rows under columns are nAn). When i try to add a column to this EMPTY dataframe though I am getting an AttributeError: ('float' object has no attribute 'isnull'", 'occurred at index 0')

I have a dataframe currently where the values look like this:

id name
nAn nAn
nAn nAn
nAn nAn

I am trying to update my dataframe to look like this: | id | name | new_column| | -------- | -------- | -------- | | nAn | nAn | N | | nAn | nAn | N | | nAn | nAn | N |

When i try to account for a null row i get the attribute error: ('float' object has no attribute 'isnull'", 'occurred at index 0')

def names(row):
    if row['name'].isnull():
        return 'N'
    elif row['name'].endswith(('-1A')):
        return 'Y'
    else:
        return 'N'

I think this is related to the fact that my entire dataframe is empty currently but I still need to add a new column into it that will at least say N whenever there are null values. I'm not 100% sure what the error is telling me either

bananas
  • 133
  • 8
  • 1
    change `row['name'].isnull()` to `pd.isna(row['name'])` – jezrael Nov 21 '22 at 06:35
  • That worked! Thank you...Any chance you could help explain why updating the code from row['name'] to pd.isna(row['name'] was the answer? A bit new with Python so learning more about the nuances would be helpful – bananas Nov 21 '22 at 06:48
  • Sure, in pandas is obviously working with arrays like `Series`, then need `mask = df['name'].isnull()` test values in column and output is boolean mask. But in your solution `row['name']` is scalar, so need `pd.isna(row['name'])` – jezrael Nov 21 '22 at 06:50

0 Answers0