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

标签 python pandas date

我读取了一个 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

关于python - 如何从 Dataframe 对象字段中提取日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55551037/

相关文章:

python - 仅在创建 MultiIndex 时 Pandas DatetimeIndex NonExistentTimeError

python - 如何使用 matplotlib 子图进行多行布局

python - 使用 BeautifulSoup 添加元标记

python - pickle 转储 Pandas DataFrame

Python 3 : Extracting Data from a . txt 文件?

Java:相对于当前(服务器)时间的日期和时间

java - 如何识别日期时间格式?

python - 删除 dataframe 列时 pandas 内存泄漏?

Python 递归错误 : Simple operation crashes with Pandas. eval()

sql-server - 在函数中设置日期优先