对pandas数据框中的列使用map()

时间:2017-03-01 10:38:25

标签: python list pandas

我的数据框中有一些列,我只想保留日期部分并删除时间部分。我列出了这些专栏:

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)

然而,这不起作用。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:13)

最简单的方法是使用lambda函数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

Dim wB As Excel.Workbook

Set RST = New ADODB.Recordset
RST.Open Sql, mCON, adOpenForwardOnly, adLockReadOnly, adCmdText
Set wB = Workbooks.Add
lRow = 6

Do Until RST.EOF = True
    Application.StatusBar = "Getting data to row: " & lRow

    For n = 0 To (RST.Fields.Count - 1)
        If Not IsNull(RST(n)) Then wB.Sheets(1).Cells(lRow, n + 1) = RST(n)
    Next n
    RST.MoveNext
    lRow = lRow + 1
Loop

wB.Sheets(1).Range("B5").Select

Application.ScreenUpdating = True
Exit Sub

答案 1 :(得分:3)

我认为您已经有了解决方案,只需将column作为参数添加到 datefunc_new 函数中:

def datefunc_new(column):
    df[column] = df[column].dt.date

map(datefunc_new, list_of_cols_to_change)

您可能还会为您的特定示例使用更多类似熊猫的代码:

def to_date(series):
    return series.dt.date

df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(to_date)