在Python中使用 pandas 分析YYYYMMDD和HH在单独的列中的日期 [英] Parse dates when YYYYMMDD and HH are in separate columns using pandas in Python
问题描述
我有一个与csv文件和解析日期时间有关的简单问题.
I have a simple question related with csv files and parsing datetime.
我有一个csv文件,如下所示:
I have a csv file that look like this:
YYYYMMDD, HH, X
20110101, 1, 10
20110101, 2, 20
20110101, 3, 30
我想使用pandas(read_csv)读取它,并将其放入由datetime索引的数据帧中.到目前为止,我已经尝试实现以下内容:
I would like to read it using pandas (read_csv) and have it in a dataframe indexed by the datetime. So far I've tried to implement the following:
import pandas as pnd
pnd.read_csv("..\\file.csv", parse_dates = True, index_col = [0,1])
我得到的结果是:
X
YYYYMMDD HH
2011-01-01 2012-07-01 10
2012-07-02 20
2012-07-03 30
如您所见,将HH转换为其他日期时的parse_dates.
As you see the parse_dates in converting the HH into a different date.
是否存在一种简单有效的方法来将"YYYYMMDD"列与"HH"列正确组合以产生类似的结果? :
Is there a simple and efficient way to combine properly the column "YYYYMMDD" with the column "HH" in order to have something like this? :
X
Datetime
2011-01-01 01:00:00 10
2011-01-01 02:00:00 20
2011-01-01 03:00:00 30
预先感谢您的帮助.
推荐答案
如果将列表传递给index_col
,则意味着您要在列表中的列之外创建层次结构索引.
If you pass a list to index_col
, it means you want to create a hierarchical index out of the columns in the list.
此外,可以将parse_dates
关键字设置为True或列表/字典.如果为True,则它将尝试将各个列解析为日期,否则将合并列以解析单个日期列.
In addition, the parse_dates
keyword can be set to either True or a list/dict. If True, then it tries to parse individual columns as dates, otherwise it combines columns to parse a single date column.
总而言之,您要做的是:
In summary, what you want to do is:
from datetime import datetime
import pandas as pd
parse = lambda x: datetime.strptime(x, '%Y%m%d %H')
pd.read_csv("..\\file.csv", parse_dates = [['YYYYMMDD', 'HH']],
index_col = 0,
date_parser=parse)
这篇关于在Python中使用 pandas 分析YYYYMMDD和HH在单独的列中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!