What is the preferred way to check that data frame exists given you have data frame name as string? I can think of:
df_name <- 'iris'
# Option 1
tryCatch(is.data.frame(get(df_name)), error=function(cond) FALSE)
# Option 2
if (exists(df_name)) is.data.frame(get(df_name)) else FALSE
Print the input DataFrame, df. Initialize a col variable with column name. Create a user-defined function check() to check if a column exists in the DataFrame. Call check() method with valid column name.
Use DataFrame. isnull(). Values. any() method to check if there are any missing data in pandas DataFrame, missing data is represented as NaN or None values in DataFrame.
Using isinstance() method. It is used to check particular data is RDD or dataframe. It returns the boolean value.
The second option can be shortened to
exists(df_name) && is.data.frame(get(df_name))
The operator &&
allows lazy evaluation, i.e., the second statement is only evaluated if the first one returns TRUE
.
exists("df_name") would give a TRUE (if the data frame exist) and FALSE (if it doesn't). So why bother? The trycatch statement in the first response did not work. It's output was FALSE all the time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With