Check if a row exists in pandas

Another way to check if a row/line exists in dataframe is using df.loc:

subDataFrame = dataFrame.loc[dataFrame[columnName] == value]

This code checks every 'value' in a given line(separated by comma), return True/False if a line exists in the dataframe

There is a short example using Stocks for the dataframe

# *****     Code for 'Check if a line exists in dataframe' using Pandas     *****

# Checks if value can be converted to a number
# Return: True/False
def isfloat(value):
  try:
    float(value)
    return True
  except:
    return False


# Example:
# list1 = ['D','C','B','A']
# list2 = ['OK','Good','82','Great']
# mergedList = [['D','OK'],['C','Good'],['B',82],['A','Great']
def getMergedListFromTwoLists(list1, list2):
    mergedList = []
    numOfColumns = min(len(list1), len(list2))
    for col in range(0, numOfColumns):
        val1 = list1[col]
        val2 = list2[col]

        # In the dataframe value stored as a number
        if isfloat(val2):
            val2 = float(val2)
        mergedList.append([val1, val2])

    return mergedList


# Returns only rows that have valuesAsArray[1] in the valuesAsArray[0]
# Example: valuesAsArray = ['Symbol','AAPL'], returns rows with 'AAPL'
def getSubDataFrame(dataFrame, valuesAsArray):
    subDataFrame = dataFrame.loc[dataFrame[valuesAsArray[0]] == valuesAsArray[1]]
    return subDataFrame




def createDataFrameAsExample():
    import pandas as pd
    data = {
        'MarketCenter': ['T', 'T', 'T', 'T'],
        'Symbol': ['AAPL', 'FB', 'AAPL', 'FB'],
        'Date': [20190101, 20190102, 20190201, 20190301],
        'Time': ['08:00:00', '08:00:00', '09:00:00', '09:00:00'],
        'ShortType': ['S', 'S', 'S', 'S'],
        'Size': [10, 10, 20, 30],
        'Price': [100, 100, 300, 200]
    }
    dfHeadLineAsArray = ['MarketCenter', 'Symbol', 'Date', 'Time', 'ShortType', 'Size','Price']
    df = pd.DataFrame(data, columns=dfHeadLineAsArray)
    return df



def adapterCheckIfLineExistsInDataFrame(originalDataFrame, headlineAsArray, line):
    dfHeadLineAsArray = headlineAsArray
    # Line example: 'T,AAPL,20190101,08:00:00,S,10,100'
    lineAsArray = line.split(',')

    valuesAsArray = getMergedListFromTwoLists(dfHeadLineAsArray, lineAsArray)
    return checkIfLineExistsInDataFrame(originalDataFrame, valuesAsArray)



def checkIfLineExistsInDataFrame(originalDataFrame,  valuesAsArray):

    if not originalDataFrame.empty:


        subDateFrame = originalDataFrame
        for value in valuesAsArray:
            if subDateFrame.empty:
                return False
            subDateFrame = getSubDataFrame(subDateFrame, value)

        if subDateFrame.empty:
            False
        else:
            return True
    return False


def testExample():
    dataFrame = createDataFrameAsExample()
    dfHeadLineAsArray = ['MarketCenter', 'Symbol', 'Date', 'Time', 'ShortType', 'Size','Price']

    # Three made up lines (not in df)
    lineToCheck1 = 'T,FB,20190102,13:00:00,S,10,100'
    lineToCheck2 = 'T,FB,20190102,08:00:00,S,60,100'
    lineToCheck3 = 'T,FB,20190102,08:00:00,S,10,150'

    # This line exists in the dataframe
    lineToCheck4 = 'T,FB,20190102,08:00:00,S,10,100'

    lineExists1 = adapterCheckIfLineExistsInDataFrame(dataFrame,dfHeadLineAsArray,lineToCheck1)
    lineExists2 = adapterCheckIfLineExistsInDataFrame(dataFrame,dfHeadLineAsArray,lineToCheck2)
    lineExists3 = adapterCheckIfLineExistsInDataFrame(dataFrame,dfHeadLineAsArray,lineToCheck3)
    lineExists4 = adapterCheckIfLineExistsInDataFrame(dataFrame,dfHeadLineAsArray,lineToCheck4)

    expected = 'False False False True'
    print('Expected:',expected)
    print('Method:',lineExists1,lineExists2,lineExists3,lineExists4)



testExample()

Click to see the dataframe Dataframe from Example


I think you need compare index values - output is True and False numpy array. And for scalar need any - check at least one True or all for check if all values are Trues:

(df.index == 'entry').any()

(df.index == 'entry').all()

Another solution from comment of John Galt:

'entry' in df.index

If need check substring:

df.index.str.contains('en').any()

Sample:

df = pd.DataFrame({'Apr 2013':[1,2,3]}, index=['entry','pdf','sum'])
print(df)
       Apr 2013
entry         1
pdf           2
sum           3

print (df.index == 'entry')
[ True False False]

print ((df.index == 'entry').any())
True
print ((df.index == 'entry').all())
False

#check columns values
print ('entry' in df)
False
#same as explicitely call columns (better readability)
print ('entry' in df.columns)
False
#check index values
print ('entry' in df.index)
True
#check columns values
print ('Apr 2013' in df)
True
#check columns values
print ('Apr 2013' in df.columns)
True

df = pd.DataFrame({'Apr 2013':[1,2,3]}, index=['entry','entry','entry'])
print(df)
       Apr 2013
entry         1
entry         2
entry         3

print (df.index == 'entry')
[ True  True  True]

print ((df.index == 'entry').any())
True
print ((df.index == 'entry').all())
True

Tags:

Python

Pandas