给定一个graphlab SFrame带有日期的列,例如:
+-------+------------+---------+-----------+
| Store | Date | Sales | Customers |
+-------+------------+---------+-----------+
| 1 | 2015-07-31 | 5263.0 | 555.0 |
| 2 | 2015-07-31 | 6064.0 | 625.0 |
| 3 | 2015-07-31 | 8314.0 | 821.0 |
| 4 | 2015-07-31 | 13995.0 | 1498.0 |
| 3 | 2015-07-20 | 4822.0 | 559.0 |
| 2 | 2015-07-10 | 5651.0 | 589.0 |
| 4 | 2015-07-11 | 15344.0 | 1414.0 |
| 5 …Run Code Online (Sandbox Code Playgroud) 我的数据集中有一个特性,它是一个 Pandas 时间戳对象。它具有(除其他外)以下属性:年、小时、星期几、月。
我可以使用一些蛮力方法基于这些属性创建新功能:
df["year"] = df["timeStamp"].apply(lambda x : x.year)
df["hour"] = df["timeStamp"].apply(lambda x : x.hour)
Run Code Online (Sandbox Code Playgroud)
. . .
但是,我想遍历一个列表:
nomtimes = ["year", "hour", "month", "dayofweek"]
for i in nomtimes:
df[i] = df["timeStamp"].apply(lambda x : x.i)
Run Code Online (Sandbox Code Playgroud)
我得到以下 AttributeError: 'Timestamp' object has no attribute 'i',我明白了为什么我会遇到这个错误。
如何让引用的字符串取消引用,以便我可以将其作为属性传递?
我有一个数据框,其中有一个名为'fecha_dato'的列.它存储日期,如'2016-05-28'.我想从fecha_dato中提取2016,05和28作为int作为名为年,月和日的新列.我使用迭代器方式,但它太慢了.有没有有效的方法来做到这一点?