我有一个 Pandas DataFrame,其中只有一个日期时间类型的列 ('publish_time
')。我只想得到时间,所以我尝试了这样的事情:
df.applymap(datetime.time)
不幸的是,我得到一个TypeError
:
TypeError: ('an integer is required', u'occurred at index publish_time')
我有点迷路了,因为我正在将 datetime
函数应用于一堆 datetime
对象,所以我不知道为什么 Python 需要一个整数.有什么建议吗?
谢谢!
最佳答案
使用 dt
访问器,您可以以矢量化方式获取时间分量:
df = pd.DataFrame({'publish_time': ['20170606 12:04', '20170606 17:02']})
df
Out:
publish_time
0 20170606 12:04
1 20170606 17:02
df['publish_time'] = pd.to_datetime(df['publish_time'])
df['publish_time'].dt.time
Out:
0 12:04:00
1 17:02:00
Name: publish_time, dtype: object
关于python - 无法将函数应用于 Pandas DataFrame 中的每个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44400300/