Python 对 Pandas 数据框中的列使用 map()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42529454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using map() for columns in a pandas dataframe
提问by Vishesh Shrivastav
I have some columns in my dataframe for which I just want to keep the date part and remove the time part. I have made a list of these columns:
我的数据框中有一些列,我只想保留日期部分并删除时间部分。我列出了这些列:
list_of_cols_to_change = ['col1','col2','col3','col4']
I have written a function for doing this. It takes a list of columns and applies dt.date to each column in the list.
我已经编写了一个函数来执行此操作。它需要一个列列表并将 dt.date 应用于列表中的每一列。
def datefunc(x):
for column in x:
df[column] = df[column].dt.date
I then call this function passing the list as parameter:
然后我调用这个函数将列表作为参数传递:
datefunc(list_of_cols_to_change )
I want to accomplish this using something like map(). Basically use a function what takes a column as parameter and makes changes to it. I then want to use map() to apply this function to the list of columns that I have. Something like this:
我想使用 map() 之类的东西来完成此操作。基本上使用一个函数,它将列作为参数并对其进行更改。然后我想使用 map() 将此函数应用于我拥有的列列表。像这样的东西:
def datefunc_new(column):
df[column] = df[column].dt.date
map(datefunc_new,list_of_cols_to_change)
This does not work however. How can I make this work ?
然而这不起作用。我怎样才能使这项工作?
回答by jezrael
The simpliest is use lambda
function with apply
:
最简单的是使用lambda
函数apply
:
df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
'col5':[5,3,6],
'col6':[7,4,3]})
print (df)
col1 col2 col3 \
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07
col4 col5 col6
0 2015-09-02 15:00:07 5 7
1 2015-09-03 15:00:07 3 4
2 2015-09-04 15:00:07 6 3
list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
col1 col2 col3 col4 col5 col6
0 2015-01-02 2015-05-02 2015-04-02 2015-09-02 5 7
1 2015-01-03 2015-05-03 2015-04-03 2015-09-03 3 4
2 2015-01-04 2015-05-04 2015-04-04 2015-09-04 6 3
回答by pansen
I think you already have the solution, just add column
as a parameter to your datefunc_newfunction:
我认为您已经有了解决方案,只需将其column
作为参数添加到您的datefunc_new函数中即可:
def datefunc_new(column):
df[column] = df[column].dt.date
map(datefunc_new, list_of_cols_to_change)
You may also use a more pandas like code for your specific example:
您还可以为您的特定示例使用更多类似熊猫的代码:
def to_date(series):
return series.dt.date
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(to_date)