Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if data frame exists

Tags:

dataframe

r

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
like image 817
Tomas Greif Avatar asked Dec 13 '13 18:12

Tomas Greif


People also ask

How do you check if a DataFrame exists or not?

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.

How do I know if a DataFrame is none?

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.

How do you check if something is a DataFrame in Python?

Using isinstance() method. It is used to check particular data is RDD or dataframe. It returns the boolean value.


2 Answers

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.

like image 166
Sven Hohenstein Avatar answered Oct 18 '22 05:10

Sven Hohenstein


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.

like image 24
user2653586 Avatar answered Oct 18 '22 04:10

user2653586