return default if pandas dataframe.loc location doesn't exist

后端 未结 2 1597
半阙折子戏
半阙折子戏 2021-02-11 14:14

I find myself often having to check whether a column or row exists in a dataframe before trying to reference it. For example I end up adding a lot of code like:

         


        
相关标签:
2条回答
  • 2021-02-11 14:47

    There is a method for Series:

    So you could do:

    df.mycol.get(myIndex, NaN)
    

    Example:

    In [117]:
    
    df = pd.DataFrame({'mycol':arange(5), 'dummy':arange(5)})
    df
    Out[117]:
       dummy  mycol
    0      0      0
    1      1      1
    2      2      2
    3      3      3
    4      4      4
    
    [5 rows x 2 columns]
    In [118]:
    
    print(df.mycol.get(2, NaN))
    print(df.mycol.get(5, NaN))
    2
    nan
    
    0 讨论(0)
  • 2021-02-11 15:01

    Python has this mentality to ask for forgiveness instead of permission. You'll find a lot of posts on this matter, such as this one.

    In Python catching exceptions is relatively inexpensive, so you're encouraged to use it. This is called the EAFP approach.

    For example:

    try:
        x = df.loc['myindex', 'mycol']
    except KeyError:
        x = mydefault
    
    0 讨论(0)
提交回复
热议问题