【发布时间】:2017-07-20 15:30:23
【问题描述】:
我的数据框中有一些列,我只想保留日期部分并删除时间部分。我已经列出了这些列:
list_of_cols_to_change = ['col1','col2','col3','col4']
我为此编写了一个函数。它需要一个列列表并将 dt.date 应用于列表中的每一列。
def datefunc(x):
for column in x:
df[column] = df[column].dt.date
然后我调用这个函数,将列表作为参数传递:
datefunc(list_of_cols_to_change )
我想使用类似 map() 的东西来完成这个任务。基本上使用将列作为参数并对其进行更改的函数。然后我想使用 map() 将此函数应用于我拥有的列列表。像这样的:
def datefunc_new(column):
df[column] = df[column].dt.date
map(datefunc_new,list_of_cols_to_change)
但这不起作用。我怎样才能做到这一点?
【问题讨论】: