Search code examples
python-3.xpandasdataframeheaderdrop

How to drop records containing cell values equals to the header in pandas


I have read in this dataframe (called df):

enter image description here

As you can see there is a record that contains the same values as the header (ltv and age).

How do I drop that record in pandas?

Data:

df = pd.DataFrame({'ltv':[34.56, 50, 'ltv', 12.3], 'age':[45,56,'age',45]})

Solution

  • Check with

    out = df[~df.eq(df.columns).any(1)]
    Out[203]: 
         ltv age
    0  34.56  45
    1     50  56
    3   12.3  45