猿问

如何选择在 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]

在这里我们可以注意到每一列都包含两个单词之间的空格,


In [7]: DT_EX.names


Out[7]: ('Year sold', 'Year Construction')

我现在选择第一列,


DT[:,f.Year sold] 

它抛出一个错误


 File "<ipython-input-5-29b1f34a6dc6>", line 1

    DT[:,f.Year sold]

                   ^

SyntaxError: invalid syntax


现在我在列名周围加上一些引号并尝试选择,


In [6]: DT[:,f.`Year sold`]                                                                                                                                                         

 File "<ipython-input-6-a007534b786e>", line 1

    DT[:,f.`Year sold`]

           ^

SyntaxError: invalid syntax

那么如何指定这些类型的列名,我可以做一些字符串操作,比如用下划线调整空间 _ Year_sold 或 Year_Construction 等。


在加载数据集时,fread() 中是否有任何选项可以处理此类列名?


将所有列转换为小写或大写名称


删除列名之间的所有特殊符号空间


如果需要,缩短列名长度。


MYYA
浏览 89回答 1
1回答

HUX布斯

这应该工作:In [1]: import datatable as dt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [2]: DT_EX = dt.Frame({'Year sold':[2000,2002,2004,2006],'Year Construction':[1990,1992,1994,1996]})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [3]: DT_EX[:, dt.f['Year sold']]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[3]:&nbsp;&nbsp; &nbsp;| Year sold-- + ---------&nbsp;0 |&nbsp; &nbsp; &nbsp; 2000&nbsp;1 |&nbsp; &nbsp; &nbsp; 2002&nbsp;2 |&nbsp; &nbsp; &nbsp; 2004&nbsp;3 |&nbsp; &nbsp; &nbsp; 2006[4 rows x 1 column]In [4]:&nbsp;&nbsp;使用f带括号的选择器还允许使用变量进行选择:In [6]: feature = 'Year sold'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [7]: DT_EX[:, dt.f[feature]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[7]:&nbsp;&nbsp; &nbsp;| Year sold-- + ---------&nbsp;0 |&nbsp; &nbsp; &nbsp; 2000&nbsp;1 |&nbsp; &nbsp; &nbsp; 2002&nbsp;2 |&nbsp; &nbsp; &nbsp; 2004&nbsp;3 |&nbsp; &nbsp; &nbsp; 2006[4 rows x 1 column]另外,如果您想用下划线替换空格,请执行以下操作:In [8]: DT_EX.names = [feat.replace(' ', '_') for feat in DT_EX.names]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [9]: DT_EX&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[9]:&nbsp;&nbsp; &nbsp;| Year_sold&nbsp; Year_Construction-- + ---------&nbsp; -----------------&nbsp;0 |&nbsp; &nbsp; &nbsp; 2000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1990&nbsp;1 |&nbsp; &nbsp; &nbsp; 2002&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1992&nbsp;2 |&nbsp; &nbsp; &nbsp; 2004&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1994&nbsp;3 |&nbsp; &nbsp; &nbsp; 2006&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1996[4 rows x 2 columns]In [10]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;希望这可以帮助。
随时随地看视频慕课网APP

相关分类

Go
我要回答