0

I have csv data as below:

x    y    date    time
2    4    4/5/2017 00:22:34
5    1    4/5/2017 00:22:50
.    .    ......    ......
.    .    .....      .....
so on

I converted date and time into one single datetime column and I want to insert that datetime back again into csv file or into dataframe.

output:

x    y    datetime
2    4    4-5-2017 00:22:34

I tried this code:

import pandas as pd
df = pd.read_csv('file.csv')
datetime = pd.to_datetime(df['date'] + ' ' + df['time'])
print(datetime)

I want to insert datetime to the df in order to write it to the csv file.

0

1 Answer 1

3

Try this:

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

or do it on the fly:

In [51]: pd.read_csv(filename, delim_whitespace=True,
                     parse_dates={'datetime':['date','time']})
Out[51]:
             datetime  x  y
0 2017-04-05 00:22:34  2  4
1 2017-04-05 00:22:50  5  1
Sign up to request clarification or add additional context in comments.

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.