Home > Software engineering >  How to use lambda with pandas correctly
How to use lambda with pandas correctly

Time:05-30

I have the colomn in DataFrame where data are:

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

I need to delete the 'Z' and 'T' letters and transform them into a format like:

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

I used lambda in my code like:

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'])

And i got next strange format:

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...

How can I change it into I wrote upper?

CodePudding user response:

You can try

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)

or convert it to datetime with pd.to_datetime

df['data_trade_start'] = pd.to_datetime(df['data_trade_start'])

CodePudding user response:

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

The problem is that you are creating a column whose values are lambda functions.

Try the following to convert a datetime column to a string in a given format

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)
  • Related