Error when trying to find a string in a column

I am trying to find an ID in a dataframe column.

If it exists then the new df will be converted to the correspondent one, but it raises an error.

Where could be the mistake?

df = pd.read_excel('somefile.xlsx')
ids = df['ID'].to_list()

for id in ids:
  if id in ids:
    df_arc = df_arc.set_index('ID')
    df_arc=df_arc.loc[id]
  else:
    print('It is not here')

This is the error I receive:

KeyError: "None of ['ID'] are in the columns"

Upvotes: 0

Views: 1144

Answers (1)

crissal
crissal

Reputation: 2647

df["ID"] will try to access the ID column - that you don't have.

If you refer to the rows index, then you can just change this statement with:

ids = df.index.to_list()

Upvotes: 1

Related Questions