如何正确使用带有 pandas 的 lambda
How to use lambda with pandas correctly
我在 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...
怎么改成我写的upper?
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)
你可以试试
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'])
我在 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...
怎么改成我写的upper?
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)
你可以试试
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'])