[Python] How to find integer index of a string in a column in pandas dataframe?

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I am importing a csv file in pandas containing data like this. Referring to following code, I want to get integer index of row containing name_to_search in column name in df1.

    name, ColB, ColC, ColD
    P1, 1,1,1
    P2, 0,1,0
    P3, 1,1,0
    ...

    df1 = pd.read_csv(filepath_or_buffer='file.csv', header=[0])
    df1['name'].str.lower()
    name_to_search = 'p1'

    row_indx1 = df1.index.get_loc(df1[df1['name'] == name_to_search].index[0]) # error line


    However, I am getting error IndexError: index 0 is out of bounds for axis 0 with size 0 in error line row. Any idea how to fix?

    Continue reading...

Compartilhe esta Página