简体   繁体   中英

Combine columns to one date time object Python

I'm trying to combine different columns into one datetime object in Python.

Currently the dataframe looks like this,

<bound method NDFrame.to_clipboard of              time  heartRate
date                        
2020-04-14  06:03         71
2020-04-14  09:03         72
2020-04-14  09:55         61
2020-04-14  09:58         67
2020-04-14  10:01         82>

I want to just have one column with one datetime object to refer too.

I'm thinking pd.to_datetime() is the way here but not sure how to combine the columns themselves.

What would be the correct way to approach this?

Solution

df['datetime_col'] = pd.to_datetime(df['date'] + ' ' + df['time'])

With details

>>> df = pd.DataFrame({'date':['2020-04-14', '2020-04-14'], 'time':['06:03', '09:03']})
>>> df
         date   time
0  2020-04-14  06:03
1  2020-04-14  09:03

>>> df['datetime_col'] = pd.to_datetime(df['date'] + ' ' + df['time'])
>>> df
         date   time        datetime_col
0  2020-04-14  06:03 2020-04-14 06:03:00
1  2020-04-14  09:03 2020-04-14 09:03:00

>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 3 columns):
 #   Column        Non-Null Count  Dtype         
---  ------        --------------  -----         
 0   date          2 non-null      object        
 1   time          2 non-null      object        
 2   datetime_col  2 non-null      datetime64[ns]
dtypes: datetime64[ns](1), object(2)
memory usage: 176.0+ bytes

You can use pandas.Series.str.cat() to concatenate strings in the Series.

df["datetime"] = pd.to_datetime(df["date"].str.cat(df['time'], sep=" "))

In Pandas, there are only these data types: boolean, integer, float, DateTime, and object. Objects are almost always strings but can be any Python object.

First, you convert the desire columns to Dataframe object to string type . After converting the columns then one can easily merge the desire columns into a single column.

For better understanding see the below code.

>>> import pandas as pd
>>> df = pd.DataFrame({'date':['2021-04-23', '2021-04-25'], 'time':['04:23', '10:33'], 'foo':[88, 999]})
>>> df
         date   time  foo
0  2021-04-23  04:23   88
1  2021-04-25  10:33  999
>>> df['datetime'] =  pd.to_datetime(df['date'] + ' ' + df['time'])
>>> df
         date   time  foo            datetime
0  2021-04-23  04:23   88 2021-04-23 04:23:00
1  2021-04-25  10:33  999 2021-04-25 10:33:00

But if we want to merge the 3 columns an error will appear because of a type mismatch. So, we have to convert the non-string type columns into string type then we can merge any column into single one.

df['column name'] = df['column name'].astype(str)

Now we can merge the columns into a single one or make a DateTime data frame.

>>> import pandas as pd
>>> df = pd.DataFrame({'date':['2021-04-23', '2021-04-25'], 'time':['04:23', '10:33'], 'foo':[88, 999]})
>>> df
         date   time  foo
0  2021-04-23  04:23   88
1  2021-04-25  10:33  999
>>> df['foo'] = df['foo'].astype(str)
>>> df['another_column'] =  df['date'] + ' ' + df['time'] + ' ' + df['foo']
>>> df
         date   time  foo        another_column
0  2021-04-23  04:23   88   2021-04-23 04:23 88
1  2021-04-25  10:33  999  2021-04-25 10:33 999

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 4 columns):
date              2 non-null object
time              2 non-null object
foo               2 non-null object
another_column    2 non-null object
dtypes: object(4)
memory usage: 192.0+ bytes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM