使用“.”访问 pandas.DataFrame 列名称在里面
Accessing a pandas.DataFrame column name with a '.' in it
我有一个 pandas 数据框 df
。其中一列是 Project.Fwd_Primer
.
我想访问该列,但是当我使用
df.Project.Fwd_Primer
我得到:
AttributeError.
是否有其他方法可以访问此列,或者我是否需要删除其中的句号?
使用[]
:
df['Project.Fwd_Primer']
样本:
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
编辑:
您还可以查看 attribute access in docs:
警告
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.
试试这个:
df['Project.Fwd_Primer']
我有一个 pandas 数据框 df
。其中一列是 Project.Fwd_Primer
.
我想访问该列,但是当我使用
df.Project.Fwd_Primer
我得到:
AttributeError.
是否有其他方法可以访问此列,或者我是否需要删除其中的句号?
使用[]
:
df['Project.Fwd_Primer']
样本:
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
编辑:
您还可以查看 attribute access in docs:
警告
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.
试试这个:
df['Project.Fwd_Primer']