使用 '.' 访问 pandas.DataFrame 列名 在里面

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

Accessing a pandas.DataFrame column name with a '.' in it

pandasselectindexingdataframeperiod

提问by Patrickc01

I have a pandas dataframe df. One of the columns is Project.Fwd_Primer.

我有一个Pandas数据框df。其中一列是Project.Fwd_Primer

I would like to access that column, however when I use df.Project.Fwd_PrimerI get:

我想访问该列,但是当我使用时, df.Project.Fwd_Primer我得到:

AttributeError.

属性错误。

Is there another way I can access this column, or do I need to get rid of the period in it?

是否有其他方法可以访问此列,或者我是否需要去掉其中的句点?

回答by jezrael

Use []:

使用[]

df['Project.Fwd_Primer']

Sample:

样本:

import pandas as pd

df = pd.DataFrame({'Project.Fwd_Primer': {0: '1', 1: '2'}})

print (df)
    Project.Fwd_Primer
0                  1
1                  2

print (df['Project.Fwd_Primer'])
0    1
1    2
Name: Project.Fwd_Primer, dtype: object

EDIT:

编辑:

You can also check attribute access in docs:

您还可以在 docs 中检查属性访问

Warning

警告

You can use this access only if the indexelement is a valid python identifier, e.g. s.1is not allowed. See here for an explanation of valid identifiers.

The attribute will not be available if it conflicts with an existing method name, e.g. s.minis not allowed.

Similarly, the attribute will not be available if it conflicts with any of the following list: index, major_axis, minor_axis, items, labels.

In any of these cases, standard indexing will still work, e.g. s['1'], s['min'], and s['index']will access the corresponding element or column.

The Series/Panel accesses are available starting in 0.13.0.

仅当索引元素是有效的 python 标识符时,您才能使用此访问,例如s.1是不允许的。有关有效标识符的说明,请参见此处。

如果该属性与现有方法名称冲突,则该属性将不可用,例如不允许使用s.min

同样,如果该属性与以下任何列表冲突,则该属性将不可用:indexmajor_axisminor_axisitemslabels

在任何这些情况下,标准索引仍然有效,例如s['1']s['min']s['index']将访问相应的元素或列。

系列/面板访问从 0.13.0 开始可用。

回答by MaxU

try this:

尝试这个:

df['Project.Fwd_Primer']