I get the columns from a data frame, what I'm trying to do is, if columns is not empty do this... if is empty then do that... but I'm receiving the error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() by using the following code:
sw_col = df_sw.columns
if sw_col.notnull():
miss = (sw_col) - (df.columns)
print(miss)
else:
df_sw = pd.concat([df, df_sw], ignore_index=True)
I would appreciate your help guys :)
About the error you got, that's because sw_col.notnull()
returns an array of boolean values and python cannot decide which element it should consider to execute if or else.
==========================================
(1) When you say columns is empty, do you mean dataframe is empty? Because, columns return the NAMES of columns. if that is the case, then you can use df.empty
to check that.
(2) If that's not the case and you are trying to see if the columns have names or not, I think your approach doesn't help since df.columns return the RangeIndex type object when there are no names for columns. So, you can then use isinstance(sw_col, pd.RangeIndex)
to see if there are names for the columns of dataframe or not. If it returns True, it means the columns have no pre-defined names.
(3) If you are trying to search for empty columns in a dataframe (columns with no elements), then you can apply df.notnull()
and it will return True for each element if it exist in the dataframe. So, then to find the columns that are COMPLETELY EMPTY without even a single element, you can just use a.any() on each column.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments