Using map() for columns in a pandas dataframe

吃可爱长大的小学妹 提交于 2021-02-17 21:19:06

问题


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.

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:

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 ?


回答1:


The simpliest is use lambda function with 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



回答2:


I think you already have the solution, just add column as a parameter to your datefunc_new function:

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)


来源:https://stackoverflow.com/questions/42529454/using-map-for-columns-in-a-pandas-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!