简体   繁体   English

我想添加两个 df 列 df['Date'] 和 df['hour'] 来创建列时间戳

[英]I want to add two df columns df['Date'] and df['hour'] to create the column timestamp

The problem is the hour column and the date column are like this:问题是小时列和日期列是这样的:

[在此处输入图片说明

Is there any way to add them to get a column starting with 2019-07-01 7:00:00 and so on有没有办法添加它们以获得以 2019-07-01 7:00:00 等开头的列

You can do:你可以做:

df['datetime'] = pd.to_datetime(df['Date']) + pd.to_timedelta('1H') * df['Hour']

# or
# df['datetime'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['Hour'], unit='H')
df['datetime'] = df[['Date', 'hour']].apply(lambda x: ' '.join(x), axis=1)

然后:

df['datetime']= pd.to_datetime(df['datetime'])


You can try something like this你可以试试这样的

df.apply(lambda t : pd.datetime.combine(t['date_column_name'],t['time_column_name']),1)

If both columns are string you can simply concatenate it as well如果两列都是字符串,您也可以简单地连接它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有两个数据框(DF1)和(DF2)。 我想替换 (DF2) 中与 (DF1) 的两列上的条件匹配的列的值 - I have two dataframes (DF1) and (DF2). I want to substitute values for a column in (DF2) that match criteria on two columns of (DF1) 根据 df 中的其他列创建 df 列 - Create df column based on other columns in df 将列添加到基于同一 DF 中其他两个列的值在 DF 中进行查找的 Pandas DF - Add column to pandas DF that does a lookup within the DF based on values of two other columns in the same DF 基于df中两个时间戳列的新列值 - New column value based on two timestamp columns in df 将pandas df中的日期和时间列转换为时间戳 - Convert date and time columns in a pandas df to a timestamp 基于包含分隔符的现有 df 列创建 df 列 - Create df columns based on an existing df column that contains delimiters 将一个'now'时间戳列添加到pandas df中 - add a 'now' timestamp column to a pandas df 如何在熊猫中加入两个DF并选择要返回的列? - How to join two DF in pandas and select the columns I want to return? 通过 **kwargs 和 *args 传递两个 df 列以使用 relativedelta 创建一个新的日期列 - pass two df columns through **kwargs and *args to create a new date column using relativedelta 创建包含两个包含列表的熊猫 df 列的字典的列 - Create column containing the dict of two pandas df columns containing lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM