PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

[FIXED] How to use lambda with pandas correctly

 November 05, 2022     dataframe, lambda, pandas, python     No comments   

Issue

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?


Solution

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


Answered By - Ynjxsjmh
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing