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.
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)
Line 12: It returns an Index
object, containing the list of column values in the DataFrame
we 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'))
Line 13: The value 1
is returned for the country 'Canada'
, since the index in Python starts from 0
.