Yes, with pandas.DataFrame.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 2006
0 ABBOTSFORD 427000 448000
1 ABERFELDIE 534000 600000
> df.set_index('Locality', inplace=True)
> df
2005 2006
Locality
ABBOTSFORD 427000 448000
ABERFELDIE 534000 600000
> df.loc['ABBOTSFORD']
2005 427000
2006 448000
Name: ABBOTSFORD, dtype: int64
> df.loc['ABBOTSFORD'][2005]
427000
> df.loc['ABBOTSFORD'].values
array([427000, 448000])
> df.loc['ABBOTSFORD'].tolist()
[427000, 448000]
Answer from Michael Hoff on Stack OverflowIs there a way to address Pandas dataframe by column index instead of name?
Using a Pandas dataframe index as values for x-axis in matplotlib? [Python 2.7][Matplotlib][Pandas]
The issue is that you passed "site2.index.values" instead of just "site2.index".
More on reddit.comloading a csv into a dataframe with a datetime as an index
You can by specifying the index_col keyword as the column position, as an int, that should be used as the index and the parse_dates keyword as True. Example:
# Pretend the dates are in the 2nd column
df = pd.read_csv('bunch_of_dated_data.csv', index_col=1, parse_dates=True) More on reddit.com Cannot reindex a non-uniquie index with a method
Videos
Yes, with pandas.DataFrame.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 2006
0 ABBOTSFORD 427000 448000
1 ABERFELDIE 534000 600000
> df.set_index('Locality', inplace=True)
> df
2005 2006
Locality
ABBOTSFORD 427000 448000
ABERFELDIE 534000 600000
> df.loc['ABBOTSFORD']
2005 427000
2006 448000
Name: ABBOTSFORD, dtype: int64
> df.loc['ABBOTSFORD'][2005]
427000
> df.loc['ABBOTSFORD'].values
array([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
> newdf
Locality ABBOTSFORD ABERFELDIE
2005 427000 534000
2006 448000 600000
then you can fetch the dataframe column values and transform them to a list:
> newdf['ABBOTSFORD'].values.tolist()
[427000, 448000]