I have a spreadsheet like this:
我有这样一个电子表格:
Locality 2005 2006 2007 2008 2009
ABBOTSFORD 427000 448000 602500 600000 638500
ABERFELDIE 534000 600000 735000 710000 775000
AIREYS INLET459000 440000 430000 517500 512500
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:
我不想手动地将列与行交换。是否有可能使用熊猫读取数据的清单如下:
data['ABBOTSFORD']=[427000,448000,602500,600000,638500]
data['ABERFELDIE']=[534000,600000,735000,710000,775000]
data['AIREYS INLET']=[459000,440000,430000,517500,512500]
2 个解决方案
#1
70
Yes, with set_index you can make Locality
your row index.
是的,有了set_index,您就可以创建本地行索引。
data.set_index('Locality', inplace=True)
If inplace=True
is not provided, set_index
returns the modified dataframe as a result.
如果不提供inplace=True,则set_index返回修改后的dataframe。
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]
#2
6
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:
可以使用set_index更改索引,如前面介绍的那样。你不需要手动地用列交换行,熊猫中有一个转置(data.T)方法可以帮你:
> 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:
然后您可以获取dataframe列值并将它们转换为列表:
> newdf['ABBOTSFORD'].values.tolist()
[427000, 448000]
#1
70
Yes, with set_index you can make Locality
your row index.
是的,有了set_index,您就可以创建本地行索引。
data.set_index('Locality', inplace=True)
If inplace=True
is not provided, set_index
returns the modified dataframe as a result.
如果不提供inplace=True,则set_index返回修改后的dataframe。
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]
#2
6
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:
可以使用set_index更改索引,如前面介绍的那样。你不需要手动地用列交换行,熊猫中有一个转置(data.T)方法可以帮你:
> 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:
然后您可以获取dataframe列值并将它们转换为列表:
> newdf['ABBOTSFORD'].values.tolist()
[427000, 448000]