Search code examples
pythonpandasindexingpandas-loc

loc get index without .index[0]


I like to look up the value of the index of my DataFrame.

idxnumber=df.loc[(df['ID'] == i[1]) & (df['date'] ==i[0])]

With this Code I get the complete line of may DataFrame. My idea was just to add .index[0] at the end of my Code. But the problem is, that sometimes there is not row for the loc function and than I will run in an error.

I need the index as an integer because in the next step I like to subtract some values of this index.


Solution

  • Just use this to get the value of the ID:

    idxnumber=df.loc[(df['ID'] == i[1]) & (df['date'] ==i[0]), 'ID']
    

    you can get the index as well by:

    df.loc[(df['ID'] == i[1]) & (df['date'] ==i[0]), 'ID'].index[0]