Skip to main content
 首页 » 编程设计

python之如何从 Dataframe 对象字段中提取日期值

2024年11月24日8java哥

我读取了一个 json 文件并将其存储在数据框中。

当我执行

df1.dtypes 

我得到存储日期的字段是 unplug_hourTime 对象

如果我使用以下方式显示 forst 记录:

df1.unplug_hourTime.head(1) 

我得到:

0    {'$date': '2017-04-01T01:00:00.000+0200'} 
Name: unplug_hourTime, dtype: object 

我不确定如何从这个结构中提取日期

我想要一个存储 01/04/2017 的变量

我也试过:

df1['Dia'] = pd.to_datetime(df1.unplug_hourTime) 

但检索到错误:

TypeError: <class 'dict'> is not convertible to datetime 

请您参考如下方法:

看起来你需要:

df1['Dia'] = pd.to_datetime(df1.unplug_hourTime.apply(lambda x: x['$date'])).dt.date