Pandas to_csv 将时间戳变成元组 [英] Pandas to_csv makes timestamps into tuples

查看:75
本文介绍了Pandas to_csv 将时间戳变成元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含时间戳列的数据框,我想将其写入 CSV 文件,但遇到了一些问题,因为它似乎正在输出一个元组.

I have a dataframe with a column of timestamps that I'd like to write to a CSV file, but running into some problems as it seems to be outputting a tuple.

print df['mydate'].loc[0], type(df['mydate'].loc[0])

收益

2009-01-01 00:00:00 <class 'pandas.tslib.Timestamp'>

当我使用 df.to_csv() 时,timestamps 被写成一个元组,因此不被 Excel 识别为日期.

When I use df.to_csv(), the timestamps are written as a tuple and thus not recognized by Excel as a date.

它在 csv 文件中的样子:

What it looks like in the csv file:

(Timestamp('2015-02-25 00:00:00'),)

推荐答案

如果您的目标是获得 Excel 识别的日期,请尝试执行 df['mydate'] = df.mydate.dt.date.astype(str) 在将其保存为 csv 之前.

If your goal is to get a date that is recognised by Excel, try doing df['mydate'] = df.mydate.dt.date.astype(str) before saving it as a csv.

如果您也想要时间,可以省略 .dt.date 部分.为了完整起见,您可以使用 .dt.strftime 根据某种自定义格式获取事物,尽管在这种特殊情况下这可能不是必需的.

If you wanted the time too, you can leave out the .dt.date part. For completeness, you can use .dt.strftime to get things according to some custom format, although this is probably not necessary in this particular case.

这篇关于Pandas to_csv 将时间戳变成元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆