Python Forum
pandas.to_datetime: Combine data from 2 columns
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas.to_datetime: Combine data from 2 columns
#1
I would like to combine data from 2 columns

The file "minimal_in.csv" is the following:

Year,Time
29.01.2001,13:31:24
29.01.2001,13:31:27
29.01.2001,13:31:29
29.01.2001,13:31:32
29.01.2001,13:31:34
I have first tried:

import pandas as pd
from datetime import datetime, timedelta

import pandas as pd

df = pd.read_csv('minimal_in.csv')
df.head(5)

df[['Year','Time']].apply(pd.to_datetime)
df.dtypes
or alternatively:

df_combine = df['Year','Time']
pd.to_datetime(df_combine)
Has someone an idea? I would be very grateful Smile
Reply
#2
Something like below?

import pandas as pd

df = pd.read_csv('minimal_in.csv')

df['Datetime'] = pd.to_datetime(df['Year'] + df['Time'], format='%d.%m.%Y%H:%M:%S')
df will look like:

Output:
Year Time Datetime 0 29.01.2001 13:31:24 2001-01-29 13:31:24 1 29.01.2001 13:31:27 2001-01-29 13:31:27 2 29.01.2001 13:31:29 2001-01-29 13:31:29 3 29.01.2001 13:31:32 2001-01-29 13:31:32 4 29.01.2001 13:31:34 2001-01-29 13:31:34
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Sorting data with pandas TheZaind 4 524 Nov-22-2021, 07:33 PM
Last Post: aserian
  Pandas Data frame column condition check based on length of the value aditi06 1 816 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  import columns of data from local csv file CatherineKan 2 1,666 May-10-2021, 05:10 AM
Last Post: ricslato
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 1,178 May-09-2021, 06:46 PM
Last Post: Pit292
  [Pandas] Write data to Excel with dot decimals manonB 1 1,039 May-05-2021, 05:28 PM
Last Post: ibreeden
  Remove extra count columns created by pandas groupby spyf8 1 1,166 Feb-10-2021, 09:19 AM
Last Post: Naheed
  Combine groupby() and shift() in pandas rama27 0 2,015 Nov-17-2020, 09:49 PM
Last Post: rama27
  Pandas: summing columns conditional on the column labels ddd2332 0 1,007 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  how to filter data frame dynamically with the columns psahay 0 1,148 Aug-24-2020, 01:10 PM
Last Post: psahay
  Can the data types be different for different columns? Robotguy 2 1,159 Aug-19-2020, 09:24 PM
Last Post: Robotguy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020