0

I'm using Pandas library.

I have three columns in dataset named 'hours', 'minutes' and 'seconds' the picture shows the three columns

I want to join the three columns to make it in time format.

For e.g the first column should read as 9:33:09

How can I do that?

1

2 Answers 2

0

Convert to timedelta and add -

pd.to_timedelta(df["hours"], unit='h') + pd.to_timedelta(df["minutes"], unit='m') + pd.to_timedelta(df["sec"], unit='S')

Viewing you example, I think that the sec column is actually microseconds, if that's the case use -

pd.to_timedelta(df["hours"], unit='h') + pd.to_timedelta(df["minutes"], unit='m') + pd.to_timedelta(df["sec"], unit='us')
Sign up to request clarification or add additional context in comments.

Comments

0

You can use string operations and pandas for this.

import pandas as pd

# Read csv

data=pd.read_csv("data.csv")


# Create a DataFrame object

df=pd.DataFrame(data,columns=["hour","mins","sec"])


# Iterate through records and print the values.

for ind in df.index: 
  hour=str(df['hour'][ind])
  min=str(df['mins'][ind])
  sec=str(df['sec'][ind])

  sec=sec[:len(sec)-4]

  if(len(sec)==1):
    sec="0"+sec

  print(hour+":"+min+":"+sec)

Output: HH:MM:SS

It appends 0 if seconds are of 1 digit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.