How do I refer to the index of my Pandas dataframe?

orome

I have a Pandas dataframe where I have designated some of the columns as indices:

planets_dataframe.set_index(['host','name'], inplace=True)

and would like to be able to refer to these indices in a variety of contexts. Using the name of an index works fine in queries

planets_dataframe.query('host == "PSR 1257 12"')

but results in an error if try to use it to get a list of the values of an index as I could when it was a column

planets_dataframe.name
#AttributeError: 'DataFrame' object has no attribute 'name'

or to use it to list results as I could when it was a "regular" column

planets_dataframe.query('30 > mass > 20 and discoveryyear > 2009')['name']
#KeyError: u'no item named name'

How do I refer to the "columns" of the dataframe that I'm using as indexes?


Before set_index:

planets_dataframe.columns
# Index([u'name', u'lastupdate', u'temperature', u'semimajoraxis', u'discoveryyear', u'calculated', u'period', u'age', u'mass', u'host', u'verification', u'transittime', u'eccentricity', u'radius', u'discoverymethod', u'inclination'], dtype='object')

After set_index:

planets_dataframe.columns
#Index([u'lastupdate', u'temperature', u'semimajoraxis', u'discoveryyear', u'calculated', u'period', u'age', u'mass', u'verification', u'transittime', u'eccentricity', u'radius', u'discoverymethod', u'inclination'], dtype='object')
BrenBarn

I think you have a slight misunderstanding of what indexes are. You don't just "designate" columns as indexes; that is, you don't just "tag" certain columns with info that says "this is an index". The index is a separate data structure that can hold data that aren't even present in the columns. If you do set_index, you move those columns into the index, so they no longer exist as regular columns. This is why you can no longer use them in the ways you mention: they aren't there anymore.

One thing you can do is, when using set_index, pass drop=False to tell it to keep the columns as columns in addition to putting them in the index (effectively copying them to the index rather than moving them), e.g., df.set_index('SomeColumn', drop=False). However, you should be aware that the index and column are still distinct, so for instance if you modify the column values this will not affect what's stored in the index.

The upshot is that indexes aren't really columns of the DataFrame, so if you want to be able to use some data as both an index and a column, you need to duplicate it in both places. There is some discussion of this issue here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I transform a dataframe in pandas without losing my index?

From Dev

How do i change a single index value in pandas dataframe?

From Dev

How do I get the maximum within a subset of my dataframe in Pandas?

From Dev

Yatzy, how do I refer to my Player-attributes?

From Dev

How do I combine columns of my dataframe to create one datetime column which I can use as my index?

From Dev

How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame

From Java

Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

From Dev

How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

From Dev

How do I transpose a pandas dataframe while preserving the index and rename keys

From Dev

How do I stop Pandas Dataframe read_json method convert my epoch to human readable string

From Dev

How do i fill the previous day's not null value in my pandas dataframe

From Dev

How do I apply my function which returns a pandas dataframe, to a range of inputs so it returns individual dataframes?

From Dev

How to add '$' to my pandas dataframe values and use a column as index?

From Dev

Pandas dataframe index causing problems when indexing subset of dataframe. How do I remove the indexes, or prevent the error from occurring?

From Dev

How do I columnwise reduce a pandas dataframe?

From Dev

How do I write a pandas dataframe to Vertica?

From Dev

How do I reshape or pivot a DataFrame in Pandas

From Dev

How do i sort the values in a dataframe in pandas?

From Dev

How do i refer to an attribute that my form is manipulating from within my form?

From Dev

How can I print out just the index of a pandas dataframe?

From Dev

How can I retrieve a row by index value from a Pandas DataFrame?

From Dev

How could I use boolean index to retrieve columns of a pandas DataFrame

From Dev

How can I advance the index of a pandas.dataframe by one quarter?

From Dev

How can I use a combination or a string as an Index of DataFrame (Pandas)?

From Dev

How do I turn pandas DataFrame groupby results into a DataFrame?

From Dev

How do I refer to the grouping of my enums? Basic chess game, wanting to refer to all black/white pieces efficiently

From Dev

how do i refer to the $index of the parent loop inside a nested ng-repeat?

From Dev

How can I normalize the data in a range of columns in my pandas dataframe

From Dev

How can I fix my Pandas Dataframe Output in csv

Related Related

  1. 1

    How can I transform a dataframe in pandas without losing my index?

  2. 2

    How do i change a single index value in pandas dataframe?

  3. 3

    How do I get the maximum within a subset of my dataframe in Pandas?

  4. 4

    Yatzy, how do I refer to my Player-attributes?

  5. 5

    How do I combine columns of my dataframe to create one datetime column which I can use as my index?

  6. 6

    How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame

  7. 7

    Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

  8. 8

    How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

  9. 9

    How do I transpose a pandas dataframe while preserving the index and rename keys

  10. 10

    How do I stop Pandas Dataframe read_json method convert my epoch to human readable string

  11. 11

    How do i fill the previous day's not null value in my pandas dataframe

  12. 12

    How do I apply my function which returns a pandas dataframe, to a range of inputs so it returns individual dataframes?

  13. 13

    How to add '$' to my pandas dataframe values and use a column as index?

  14. 14

    Pandas dataframe index causing problems when indexing subset of dataframe. How do I remove the indexes, or prevent the error from occurring?

  15. 15

    How do I columnwise reduce a pandas dataframe?

  16. 16

    How do I write a pandas dataframe to Vertica?

  17. 17

    How do I reshape or pivot a DataFrame in Pandas

  18. 18

    How do i sort the values in a dataframe in pandas?

  19. 19

    How do i refer to an attribute that my form is manipulating from within my form?

  20. 20

    How can I print out just the index of a pandas dataframe?

  21. 21

    How can I retrieve a row by index value from a Pandas DataFrame?

  22. 22

    How could I use boolean index to retrieve columns of a pandas DataFrame

  23. 23

    How can I advance the index of a pandas.dataframe by one quarter?

  24. 24

    How can I use a combination or a string as an Index of DataFrame (Pandas)?

  25. 25

    How do I turn pandas DataFrame groupby results into a DataFrame?

  26. 26

    How do I refer to the grouping of my enums? Basic chess game, wanting to refer to all black/white pieces efficiently

  27. 27

    how do i refer to the $index of the parent loop inside a nested ng-repeat?

  28. 28

    How can I normalize the data in a range of columns in my pandas dataframe

  29. 29

    How can I fix my Pandas Dataframe Output in csv

HotTag

Archive