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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:06:38  来源:igfitidea点击:

Could pandas use column as index?

pythonexcelpandas

提问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 Localityyour row index.

是的,使用set_index您可以制作Locality行索引。

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

If inplace=Trueis not provided, set_indexreturns 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.Tpandas 中有一个 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:

这是我的解决方案:

  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 output sc

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

    sc2

  1. 首先,导入pandas作为pd: import pandas as pd

  2. 使用pd.read_excel()读入文件名(如果您在电子表格中有数据)并通过指定 index_col 参数将索引设置为“Locality”。

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

    在此阶段,如果您收到“没有名为 xlrd 的模块”错误,请使用pip install xlrd.

  3. 对于目视检查,读取数据帧使用df.head()它将打印以下输出 sc

  4. 现在您可以获取数据帧所需列的值并打印它

    sc2