Python 大熊猫可以使用列作为索引吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38542419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could pandas use column as index?
提问by Chan Austin
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:
我不想手动交换列与行。是否可以使用 Pandas 将数据读取到列表中,如下所示:
data['ABBOTSFORD']=[427000,448000,602500,600000,638500]
data['ABERFELDIE']=[534000,600000,735000,710000,775000]
data['AIREYS INLET']=[459000,440000,430000,517500,512500]
回答by Michael Hoff
Yes, with set_indexyou can make Locality
your row index.
是的,使用set_index您可以制作Locality
行索引。
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
返回修改后的数据帧作为结果。
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]
回答by famargar
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
pandas 中有一个 transpose( ) 方法可以为您完成:
> 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]
回答by Steffi Keran Rani J
You can set the column index using index_colparameter available while reading from spreadsheet in Pandas.
您可以在从 Pandas 中的电子表格读取时使用可用的index_col参数设置列索引。
Here is my solution:
这是我的解决方案:
Firstly, import pandas as pd:
import pandas as pd
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
.For visual inspection, read the dataframe using
df.head()
which will print the following outputNow you can fetch the values of the desired columns of the dataframe and print it