访问 Pandas 数据框中的第一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45484323/
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-09-14 04:10:06  来源:igfitidea点击:

Access 1st column in Pandas dataframe

python-3.xpandas

提问by Y.S.

How do I access the first column in this dataframe? If I refer to it by the column name ('Group11...'), I get an error 'Not in index'.

如何访问此数据框中的第一列?如果我通过列名('Group11...')引用它,我会收到一个错误“不在索引中”。

First Column

第一列

回答by Mohamed Ali JAMAOUI

What you are refering to is the index of the dataframe. So, if your dataframe is called df, you can access the index using df.index.

您所指的是数据框的索引。因此,如果您的数据帧被调用df,您可以使用df.index.

Otherwise, if you want to refer to the as a column, you need to turn it into a column before using pandas.DataFrame.reset_index.

否则,如果要将 引用为列,则需要在使用pandas.DataFrame.reset_index之前将其转换为列。

reproducible example:

可重现的例子:

Here's a reproducible example showing the two methods of accessing the index:

这是一个可重现的示例,显示了访问索引的两种方法:

from StringIO import StringIO 
import pandas as pd 

data = """Group11.Primary.Phrase|count|num_cat
CP|4|4
DA|1|1
FW|7|7
"""

df = pd.read_csv(StringIO(data), sep="|", index_col=0)
print("here's how the dataframe looks like") 
print(df.head())

print("here's how to access the index") 
print(df.index)

print("if you want to turn the index values into a list")
print(list(df.index))

print("you can also reset_index as a column and access it") 
df = df.reset_index()
print(df["Group11.Primary.Phrase"])

Running the above code, gives you the following output:

运行上面的代码,为您提供以下输出:

here's how the dataframe looks like
                        count  num_cat
Group11.Primary.Phrase                
CP                          4        4
DA                          1        1
FW                          7        7

here's how to access the index
Index([u'CP', u'DA', u'FW'], dtype='object', name=u'Group11.Primary.Phrase')

if you want to turn the index values into a list
['CP', 'DA', 'FW']

you can also reset_index as a column and access it
0    CP
1    DA
2    FW
Name: Group11.Primary.Phrase, dtype: object

回答by Marcel Flygare

iloc return data based on a numeric index, here all rows for the first (python 0-indexed) column.

iloc 基于数字索引返回数据,这里是第一列(python 0 索引)的所有行。

df.iloc[:,0]

回答by Bharath

You can reset the index and then access column by the column name if you want to access it using column name. i.e

如果您想使用列名访问它,您可以重置索引,然后通过列名访问列。IE

If you have a dataframe like

如果你有一个像

                        count  num_cat
Group11.Primary.Phrase                
CP                          4        4
DA                          1        1
FW                          7        7

Then after resetting index when you access the column by its name then

然后在按名称访问列时重置索引后

df = df.reset_index()
df['Group11.Primary.Phrase']

Output:

输出:

0    CP
1    DA
2    FW

回答by J'e

Here is a link to the docs: Indexing and Selecting DataIn your case, you would index df['Group11']

这是文档的链接:索引和选择数据在您的情况下,您将索引df['Group11']

In [9]: df
Out[9]: 
               A         B         C         D
2000-01-01  0.469112 -0.282863 -1.509059 -1.135632
2000-01-02  1.212112 -0.173215  0.119209 -1.044236
2000-01-03 -0.861849 -2.104569 -0.494929  1.071804

In [12]: df[['A', 'B']]
Out[12]: 
               A         B
2000-01-01 -0.282863  0.469112
2000-01-02 -0.173215  1.212112
2000-01-03 -2.104569 -0.861849