无效命名列上的属性访问 [英] Attribute access on invalidly named columns
问题描述
我使用Pandas 0.11.0,试图从具有以下结构的CSV文件中读取数据:
Using Pandas 0.11.0, I am trying to read in data from a CSV file with the following structure:
Date/Time Data1 Data2
5/10/13 23 17.0
5/10/14 20 17.1
5/10/15 27 17.3
In order to create a new column based on existing data, I would use attribute access of the fashion:
df["Result"] = 2.0 * df.Data2
但是,由于日期/时间"不是有效的属性名称,因此建议根据数据/时间"列中的数据创建新列的建议方法是什么?我希望在使用read_csv方法时不必手动指定所有列名.
However, because "Date/Time" is not a valid attribute name, what is the recommended way to create a new column based on the data in the "Data/Time" column? I would prefer not to have to manually specify all column names when using the read_csv method.
推荐答案
使用df['Date/Time']
.选择列df.column_name
的属性访问样式只是df['column_name']
的便捷快捷方式.如果您的列名不是有效的Python标识符,如日期/时间"中一样.您可以更改名称,也可以使用长格式.
Use df['Date/Time']
. The attribute access style of selecting a column, df.column_name
, is merely a convenient shortcut for df['column_name']
. It is simply not possible to use this convenience when your column names are not valid Python identifiers, as in 'Date/Time'. You can change the name, or you can use the long form.
这篇关于无效命名列上的属性访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!