Could pandas use column as index? Could pandas use column as index? python python

Could pandas use column as index?


Yes, with set_index you can make Locality your row index.

data.set_index('Locality', inplace=True)

If inplace=True is not provided, set_index returns the modified dataframe as a result.

Example:

> import pandas as pd> df = pd.DataFrame([['ABBOTSFORD', 427000, 448000],                     ['ABERFELDIE', 534000, 600000]],                    columns=['Locality', 2005, 2006])> df     Locality    2005    20060  ABBOTSFORD  427000  4480001  ABERFELDIE  534000  600000> df.set_index('Locality', inplace=True)> df              2005    2006Locality                  ABBOTSFORD  427000  448000ABERFELDIE  534000  600000> df.loc['ABBOTSFORD']2005    4270002006    448000Name: ABBOTSFORD, dtype: int64> df.loc['ABBOTSFORD'][2005]427000> df.loc['ABBOTSFORD'].valuesarray([427000, 448000])> df.loc['ABBOTSFORD'].tolist()[427000, 448000]


You can change the index as explained already using set_index.You don't need to manually swap rows with columns, there is a transpose (data.T) method in pandas that does it for you:

> df = pd.DataFrame([['ABBOTSFORD', 427000, 448000],                    ['ABERFELDIE', 534000, 600000]],                    columns=['Locality', 2005, 2006])> newdf = df.set_index('Locality').T> newdfLocality    ABBOTSFORD  ABERFELDIE2005        427000      5340002006        448000      600000

then you can fetch the dataframe column values and transform them to a list:

> newdf['ABBOTSFORD'].values.tolist()[427000, 448000]


You can set the column index using index_col parameter available while reading from spreadsheet in Pandas.

Here is my solution:

  1. Firstly, import pandas as pd: import pandas as pd

  2. Read in filename using pd.read_excel() (if you have your data in a spreadsheet) and set the index to 'Locality' by specifying the index_col parameter.

    df = pd.read_excel('testexcel.xlsx', index_col=0)

    At this stage if you get a 'no module named xlrd' error, install it using pip install xlrd.

  3. For visual inspection, read the dataframe using df.head() which will print the following outputsc

  4. Now you can fetch the values of the desired columns of the dataframe and print it

    sc2