How to get the index of a column in pandas
In pandas, a DataFrame is a two-dimensional table-like structure composed of rows and columns. It is possible to access the list of columns using the .columns attribute.
Example
Let's look at an example.
In the snippet below, we'll create a DataFrame where the columns are country names, and the values are different item prices in each country.
from pandas import DataFramemy_dictionary = {"Egypt": [100, 200, 300],"Canada": [300, 500, 700],"US": [100, 400, 800]}my_dataframe = DataFrame(my_dictionary)print(my_dataframe)print(my_dataframe.columns)
Explanation
Line 12: It returns an
Indexobject, containing the list of column values in theDataFramewe created.
Now, let's say we want to access the column index corresponding to the country 'Canada'. We can use the method .get_loc(<column_name>) that is applied on an Index object retrieved using .columns.
from pandas import DataFramemy_dictionary = {"Egypt": [100, 200, 300],"Canada": [300, 500, 700],"US": [100, 400, 800]}my_dataframe = DataFrame(my_dictionary)print(my_dataframe)print(my_dataframe.columns)print(my_dataframe.columns.get_loc('Canada'))
Explanation
Line 13: The value
1is returned for the country'Canada', since the index in Python starts from0.