Testing if a pandas DataFrame exists

前端 未结 2 1390
有刺的猬
有刺的猬 2021-02-03 17:44

In my code, I have several variables which can either contain a pandas DataFrame or nothing at all. Let\'s say I want to test and see if a certain DataFrame has been created ye

2条回答
  •  灰色年华
    2021-02-03 18:13

    Option 1 (my preferred option)

    This is @Ami Tavory's

    Please select his answer if you like this approach

    It is very idiomatic python to initialize a variable with None then check for None prior to doing something with that variable.

    df1 = None
    
    if df1 is not None:
        print df1.head()
    

    Option 2

    However, setting up an empty dataframe isn't at all a bad idea.

    df1 = pd.DataFrame()
    
    if not df1.empty:
        print df1.head()
    

    Option 3

    Just try it.

    try:
        print df1.head()
    # catch when df1 is None
    except AttributeError:
        pass
    # catch when it hasn't even been defined
    except NameError:
        pass
    

    Timing

    When df1 is in initialized state or doesn't exist at all

    When df1 is a dataframe with something in it

    df1 = pd.DataFrame(np.arange(25).reshape(5, 5), list('ABCDE'), list('abcde'))
    df1
    

提交回复
热议问题