create another index for pandas DataFrame like CREATE INDEX in SQL

Viewed 85

Can I create another index on an existing column of pandas DataFrame? Just like what CREATE INDEX in SQL does. For example: My DataFrame has two columns id_a and id_b, both of them are unique for each row, and I'd like to index rows sometimes with id_a while other times with id_b (so I think MultiIndex won't work for me). I want these operations to be fast, so "index" must be created for both id_a and id_b.

Is it possible I can do this in pandas currently?

1 Answers

You can't have 2 indices in a Pandas DataFrame object.

You will have to workaround this limitation, for example:

  • by code logic,
  • using other Pandas features,
  • using columns and flags depending which column needs to be used to index a given row

The operations should be fast. For additional performance, you can adjust the dtypes according to your needs. To match hashmap lookups or similar, you will have to add more thought into your use case and perhaps use a different logical approach with a separate mapping/dict or similar.

Related