Check if a value exists in pandas dataframe index

前端 未结 6 803
轮回少年
轮回少年 2020-12-23 13:17

I am sure there is an obvious way to do this but cant think of anything slick right now.

Basically instead of raising exception I would like to get True

相关标签:
6条回答
  • 2020-12-23 13:28

    Code below does not print boolean, but allows for dataframe subsetting by index... I understand this is likely not the most efficient way to solve the problem, but I (1) like the way this reads and (2) you can easily subset where df1 index exists in df2:

    df3 = df1[df1.index.isin(df2.index)]
    

    or where df1 index does not exist in df2...

    df3 = df1[~df1.index.isin(df2.index)]
    
    0 讨论(0)
  • 2020-12-23 13:29

    Just for reference as it was something I was looking for, you can test for presence within the values or the index by appending the ".values" method, e.g.

    g in df.<your selected field>.values
    g in df.index.values
    

    I find that adding the ".values" to get a simple list or ndarray out makes exist or "in" checks run more smoothly with the other python tools. Just thought I'd toss that out there for people.

    0 讨论(0)
  • 2020-12-23 13:31

    This should do the trick

    'g' in df.index
    
    0 讨论(0)
  • 2020-12-23 13:31
    df = pandas.DataFrame({'g':[1]}, index=['isStop'])
    
    #df.loc['g']
    
    if 'g' in df.index:
        print("find g")
    
    if 'isStop' in df.index:
        print("find a") 
    
    0 讨论(0)
  • 2020-12-23 13:33

    with DataFrame: df_data

    >>> df_data
      id   name  value
    0  a  ampha      1
    1  b   beta      2
    2  c     ce      3
    

    I tried:

    >>> getattr(df_data, 'value').isin([1]).any()
    True
    >>> getattr(df_data, 'value').isin(['1']).any()
    True
    

    but:

    >>> 1 in getattr(df_data, 'value')
    True
    >>> '1' in getattr(df_data, 'value')
    False
    

    So fun :D

    0 讨论(0)
  • 2020-12-23 13:37

    Multi index works a little different from single index. Here are some methods for multi-indexed dataframe.

    df = pd.DataFrame({'col1': ['a', 'b','c', 'd'], 'col2': ['X','X','Y', 'Y'], 'col3': [1, 2, 3, 4]}, columns=['col1', 'col2', 'col3'])
    df = df.set_index(['col1', 'col2'])
    

    in df.index works for the first level only when checking single index value.

    'a' in df.index     # True
    'X' in df.index     # False
    

    Check df.index.levels for other levels.

    'a' in df.index.levels[0] # True
    'X' in df.index.levels[1] # True
    

    Check in df.index for an index combination tuple.

    ('a', 'X') in df.index  # True
    ('a', 'Y') in df.index  # False
    
    0 讨论(0)
提交回复
热议问题