Python 3 / Pandas
I am trying to use a function to check the values of various columns in a dataframe, and select only the value from the column that is not NaN.
The data is structured so there is one main column df['C1']
that I want to populate based on the value in one of the next four columns, df['C2'], df['C3'], df['C4']
and df['C5']
. When I observe the data, I see that in the rows df['C2'], df['C3'], df['C4']
and df['C5']
, every column has a value that is NaN except for one column which has a text value. This is true for all rows in the dataframe. I am trying to write a function that will be applied to the dataframe to find the column which has a text value, and copy that value from the column into df['C1']
.
Here is the function I wrote:
def get_component(df):
if ~df['C2'].isna():
return df['C2']
elif ~df['C3'].isna():
return df['C3']
elif ~df['C4'].isna():
return df['C4']
elif ~df['C5'].isna():
return df['C5']
df['C1'] = df.apply(get_component, axis=1)
But I get the following error:
AttributeError: ("'float' object has no attribute 'isna'", 'occurred at index 0')
Any ideas on how to fix this error so I can achieve this objective? Is there another method to achieve the same result?
Thanks for the help!