【发布时间】:2017-12-13 15:59:14
【问题描述】:
考虑这个简单的例子
df = pd.DataFrame({'dt_one': ['2015-01-01', '2016-02-02'],
'dt_two': ['2015-01-01', '2016-02-02'],
'other_col': [1, 2]})
df
Out[30]:
dt_one dt_two other_col
0 2015-01-01 2015-01-01 1
1 2016-02-02 2016-02-02 2
我想将pd.to_datetime 应用于所有包含dt_ 的列
我可以通过filter轻松做到这一点
df.filter(regex = 'dt_').apply(lambda x: pd.to_datetime(x))
Out[33]:
dt_one dt_two
0 2015-01-01 2015-01-01
1 2016-02-02 2016-02-02
但是,如何在原始数据框中分配这些值? 正在做:
df.filter(regex = 'dt_') = df.filter(regex = 'dt_').apply(lambda x: pd.to_datetime(x))
File "<ipython-input-34-412d88939494>", line 1
df.filter(regex = 'dt_') = df.filter(regex = 'dt_').apply(lambda x: pd.to_datetime(x))
SyntaxError: can't assign to function call
没用
谢谢!
【问题讨论】: