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

霸气de小男生 提交于 2019-12-10 12:56:47

问题


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

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

AttributeError.

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


回答1:


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:

Warning

You can use this access only if the index element is a valid python identifier, e.g. s.1 is 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.min is 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.




回答2:


try this:

df['Project.Fwd_Primer']


来源:https://stackoverflow.com/questions/37883798/accessing-a-pandas-dataframe-column-name-with-a-in-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!