1

I have a dataframe with multiple columns of same name, which causes .loc to fail when accessing by column name.

df = pd.DataFrame([['abc', 'xyz'], ['abc', 'xyz'], ['xyz', 'abc']], columns=['max_speed', 'max_speed'])

I am trying the following method mentioned in this answer but its not successful.

df.loc[df.columns[0] == 'abc']

df.loc[df.columns[1].str.contains('xyz')]
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Murtaza Haji
  • 1,093
  • 1
  • 13
  • 32

1 Answers1

3

In pandas is best not working with duplicated columns names.

If need select by position columns names use DataFrame.iloc:

df[df.iloc[:, 0] == 'abc']
df[df.iloc[:, 1].str.contains('xyz')]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Yes , I know :P. This is a special case stemming from this question. https://stackoverflow.com/questions/61674725/appending-multiple-rows-into-a-single-row-in-pandas – Murtaza Haji May 25 '20 at 07:13