Search code examples
pythonpandasindexingsliceambiguous

How to test a specific value in last row of dataframe (using -1)


I have a continually updating dataframe (minute by minute) with a new row appended every minute. I wish to undertake a conditional test on the last value in the dataframe in a specific column. I've been trying to use [-1:] to specify the last row and the ['columnname']. However this returns a series and then the error 'The truth value of a Series is ambiguous...etc'. How to refer to a value in the last row of a specific column and conditionally test it?

    import pandas as pd
    data = {'D' : (1, 2, 3, 4, 5, 6),
            'O' : (4, 5, 3, 7, 9, 1),
            'OS' : (6, 1, 2, 0, 6, 4),
            'MAC' : (3, 6, 8, 6, 7, 9)}
    df = pd.DataFrame(data)

the simplified dataframe

then to test the last row and the 'MAC' column:

    if df[-1:]['MAC'] == 9:
        print('OK - It works now!')

returns a 'ValueError: The truth value of a Series is ambiguous...' I've tried numerous formulations using at, iat, get_value, values, loc, iloc, index, The simplest formulation:

    df[-1:]['MAC']

returns a series (which includes the index and the actual value):

    5    9
    Name: MAC, dtype: int64

Surely this is a foolishly simple problem...(on my part)...Thanks


Solution

  • For scalars is possible convert Series to numpy array and select last value:

    if df['MAC'].values[-1:] == 9:
        print('OK - It works now!')
    

    Or select by positions with -1 for last row and Index.get_loc for position of column MAC:

    if df.iloc[-1, df.columns.get_loc('MAC')] == 9:
        print('OK - It works now!')
    

    Or select by labels by DataFrame.loc - but necessary use last value of index:

    if df.loc[df.index[-1], 'MAC'] == 9:
        print('OK - It works now!')