如何正确使用 lambda 和 pandas

问题描述 投票:0回答:2

我在 DataFrame 中有一列,其中数据是:

1      2017-07-14T04:00:00.3760000Z
2      2013-10-22T23:09:46.8630000Z
3      2015-01-14T16:07:05.0000000Z
4      2011-09-13T13:53:36.0000000Z
                   ...
332    2018-03-25T07:00:01.0000000Z
333    2019-04-08T15:21:02.0000000Z
334    2017-09-17T11:10:12.5100000Z
335    2017-12-22T07:31:16.0000000Z
336    2020-05-05T13:01:20.8670000Z

我需要删除“Z”和“T”字母并将它们转换为如下格式:

2018-05-23 17:26:19 and so on...

我在代码中使用了 lambda,如下所示:

import pandas as pd

    df = pd.read_csv('All_Markets.csv')
    print(df.dtypes)
    df['data_trade_start'] = [lambda x: x[:19].replace('T', ' ') for x in df['data_trade_start']]
    print(df['data_trade_start'])

我得到了这种奇怪的格式:

1      <function <listcomp>.<lambda> at 0x00000126690...

2      <function <listcomp>.<lambda> at 0x00000126690...
3      <function <listcomp>.<lambda> at 0x00000126690...
4      <function <listcomp>.<lambda> at 0x00000126690...
                             ...
332    <function <listcomp>.<lambda> at 0x00000126690...
333    <function <listcomp>.<lambda> at 0x00000126690...
334    <function <listcomp>.<lambda> at 0x00000126690...
335    <function <listcomp>.<lambda> at 0x00000126690...
336    <function <listcomp>.<lambda> at 0x00000126690...

如何改成我上面写的?

python pandas dataframe lambda
2个回答
0
投票

你可以试试

df['data_trade_start'] = [x[:19].replace('T', ' ') for x in df['data_trade_start']]
# or
df['col'] = df['col'].replace(['T', 'Z'], [' ', ''], regex=True)

或使用

pd.to_datetime

将其转换为日期时间
df['data_trade_start'] = pd.to_datetime(df['data_trade_start'])

0
投票

df['data_trade_start'] = [lambda x: x[:19].replace('T', ' ') for x in df['data_trade_start']]

问题是您正在创建一个值为 lambda 函数的列。

尝试以下操作将日期时间列转换为给定格式的字符串

import pandas as pd

df = pd.read_csv('All_Markets.csv', parse_dates=['data_trade_start'])

df['data_trade_start'] = df['data_trade_start'].dt.strftime("%Y-%m-%d %H:%M:%S")

print(df)
© www.soinside.com 2019 - 2024. All rights reserved.