I have a spreadsheet like this:
JavaScript
x
6
1
Locality 2005 2006 2007 2008 2009
2
3
ABBOTSFORD 427000 448000 602500 600000 638500
4
ABERFELDIE 534000 600000 735000 710000 775000
5
AIREYS INLET459000 440000 430000 517500 512500
6
I don’t want to manually swap the column with the row. Could it be possible to use pandas reading data to a list as this:
JavaScript
1
4
1
data['ABBOTSFORD']=[427000,448000,602500,600000,638500]
2
data['ABERFELDIE']=[534000,600000,735000,710000,775000]
3
data['AIREYS INLET']=[459000,440000,430000,517500,512500]
4
Advertisement
Answer
Yes, with pandas.DataFrame.set_index
you can make 'Locality'
your row index.
JavaScript
1
2
1
data.set_index('Locality', inplace=True)
2
If inplace=True
is not provided, set_index
returns the modified dataframe as a result.
Example:
JavaScript
1
31
31
1
> import pandas as pd
2
> df = pd.DataFrame([['ABBOTSFORD', 427000, 448000],
3
['ABERFELDIE', 534000, 600000]],
4
columns=['Locality', 2005, 2006])
5
6
> df
7
Locality 2005 2006
8
0 ABBOTSFORD 427000 448000
9
1 ABERFELDIE 534000 600000
10
11
> df.set_index('Locality', inplace=True)
12
> df
13
2005 2006
14
Locality
15
ABBOTSFORD 427000 448000
16
ABERFELDIE 534000 600000
17
18
> df.loc['ABBOTSFORD']
19
2005 427000
20
2006 448000
21
Name: ABBOTSFORD, dtype: int64
22
23
> df.loc['ABBOTSFORD'][2005]
24
427000
25
26
> df.loc['ABBOTSFORD'].values
27
array([427000, 448000])
28
29
> df.loc['ABBOTSFORD'].tolist()
30
[427000, 448000]
31