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
  Parsing "aTimeLogger" Android app data to graphs using pandas Drone4four 8 3,037 Jun-23-2024, 07:12 AM
Last Post: Drone4four
  Grouping in pandas/multi-index data frame Aleqsie 3 2,190 Jan-06-2024, 03:55 PM
Last Post: deanhystad
Smile How to further boost the data read write speed using pandas tjk9501 1 1,970 Nov-14-2022, 01:46 PM
Last Post: jefsummers
Thumbs Up can't access data from URL in pandas/jupyter notebook aaanoushka 1 2,525 Feb-13-2022, 01:19 PM
Last Post: jefsummers
Question Sorting data with pandas TheZaind 4 3,269 Nov-22-2021, 07:33 PM
Last Post: aserian
  Pandas Data frame column condition check based on length of the value aditi06 1 3,674 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  import columns of data from local csv file CatherineKan 2 4,565 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 4,317 May-09-2021, 06:46 PM
Last Post: Pit292
  [Pandas] Write data to Excel with dot decimals manonB 1 7,888 May-05-2021, 05:28 PM
Last Post: ibreeden
  Remove extra count columns created by pandas groupby spyf8 1 3,703 Feb-10-2021, 09:19 AM
Last Post: Naheed

Forum Jump:

User Panel Messages

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