如何在 pydatatable 中使用未格式化的名称创建 select 列?
How to select columns created with unformatted names in pydatatable?
我创建了一个数据表,
DT_EX = dt.Frame({'Year sold':[2000,2002,2004,2006],'Year Construction':[1990,1992,1994,1996]})
及其视图
Out[4]:
| Year sold Year Construction
-- + --------- -----------------
0 | 2000 1990
1 | 2002 1992
2 | 2004 1994
3 | 2006 1996
[4 rows x 2 columns]
在这里我们可以注意到,每一列在两个单词之间包含一个 space,
In [7]: DT_EX.names
Out[7]: ('Year sold', 'Year Construction')
我现在 select 正在写第一栏,
DT[:,f.Year sold]
正在抛出错误
File "<ipython-input-5-29b1f34a6dc6>", line 1
DT[:,f.Year sold]
^
SyntaxError: invalid syntax
现在我在列名周围加上一些引号并尝试 select,
In [6]: DT[:,f.`Year sold`]
File "<ipython-input-6-a007534b786e>", line 1
DT[:,f.`Year sold`]
^
SyntaxError: invalid syntax
那么如何指定这些类型的列名,我可以做一些字符串操作,比如用下划线调整 space _ Year_sold 或 Year_Construction 等
在加载数据集时,fread() 中是否有任何选项可以处理此类列名?
将所有列转换为小写或大写名称
去掉列名之间的所有特殊符号space
如果需要,缩短列名长度。
这应该有效:
In [1]: import datatable as dt
In [2]: DT_EX = dt.Frame({'Year sold':[2000,2002,2004,2006],'Year Construction':[1990,1992,1994,1996]})
In [3]: DT_EX[:, dt.f['Year sold']]
Out[3]:
| Year sold
-- + ---------
0 | 2000
1 | 2002
2 | 2004
3 | 2006
[4 rows x 1 column]
In [4]:
使用带括号的 f
选择器也允许使用变量进行选择:
In [6]: feature = 'Year sold'
In [7]: DT_EX[:, dt.f[feature]]
Out[7]:
| Year sold
-- + ---------
0 | 2000
1 | 2002
2 | 2004
3 | 2006
[4 rows x 1 column]
此外,如果您想用下划线替换空格,只需执行以下操作:
In [8]: DT_EX.names = [feat.replace(' ', '_') for feat in DT_EX.names]
In [9]: DT_EX
Out[9]:
| Year_sold Year_Construction
-- + --------- -----------------
0 | 2000 1990
1 | 2002 1992
2 | 2004 1994
3 | 2006 1996
[4 rows x 2 columns]
In [10]:
希望这对您有所帮助。
我创建了一个数据表,
DT_EX = dt.Frame({'Year sold':[2000,2002,2004,2006],'Year Construction':[1990,1992,1994,1996]})
及其视图
Out[4]:
| Year sold Year Construction
-- + --------- -----------------
0 | 2000 1990
1 | 2002 1992
2 | 2004 1994
3 | 2006 1996
[4 rows x 2 columns]
在这里我们可以注意到,每一列在两个单词之间包含一个 space,
In [7]: DT_EX.names
Out[7]: ('Year sold', 'Year Construction')
我现在 select 正在写第一栏,
DT[:,f.Year sold]
正在抛出错误
File "<ipython-input-5-29b1f34a6dc6>", line 1
DT[:,f.Year sold]
^
SyntaxError: invalid syntax
现在我在列名周围加上一些引号并尝试 select,
In [6]: DT[:,f.`Year sold`]
File "<ipython-input-6-a007534b786e>", line 1
DT[:,f.`Year sold`]
^
SyntaxError: invalid syntax
那么如何指定这些类型的列名,我可以做一些字符串操作,比如用下划线调整 space _ Year_sold 或 Year_Construction 等
在加载数据集时,fread() 中是否有任何选项可以处理此类列名?
将所有列转换为小写或大写名称
去掉列名之间的所有特殊符号space
如果需要,缩短列名长度。
这应该有效:
In [1]: import datatable as dt
In [2]: DT_EX = dt.Frame({'Year sold':[2000,2002,2004,2006],'Year Construction':[1990,1992,1994,1996]})
In [3]: DT_EX[:, dt.f['Year sold']]
Out[3]:
| Year sold
-- + ---------
0 | 2000
1 | 2002
2 | 2004
3 | 2006
[4 rows x 1 column]
In [4]:
使用带括号的 f
选择器也允许使用变量进行选择:
In [6]: feature = 'Year sold'
In [7]: DT_EX[:, dt.f[feature]]
Out[7]:
| Year sold
-- + ---------
0 | 2000
1 | 2002
2 | 2004
3 | 2006
[4 rows x 1 column]
此外,如果您想用下划线替换空格,只需执行以下操作:
In [8]: DT_EX.names = [feat.replace(' ', '_') for feat in DT_EX.names]
In [9]: DT_EX
Out[9]:
| Year_sold Year_Construction
-- + --------- -----------------
0 | 2000 1990
1 | 2002 1992
2 | 2004 1994
3 | 2006 1996
[4 rows x 2 columns]
In [10]:
希望这对您有所帮助。