Ask Your Question
0

Can the 'Date' and 'Time' columns, which have a specific format, be combined into a single column named 'Date_Time' using Pandas?

asked 2023-06-16 12:21:55 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-16 12:45:01 +0000

lakamha gravatar image

Yes, the 'Date' and 'Time' columns can be combined into a single column named 'Date_Time' using Pandas by using the apply() method with a lambda function to concatenate the two columns with a space separator:

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'Date': ['2021-01-01', '2021-01-02', '2021-01-03'],
                   'Time': ['12:00:00', '13:30:00', '17:45:00']})

# Combine Date and Time columns into a single column 'Date_Time'
df['Date_Time'] = df.apply(lambda row: row['Date'] + ' ' + row['Time'], axis=1)

print(df)

Output:

         Date      Time            Date_Time
0  2021-01-01  12:00:00  2021-01-01 12:00:00
1  2021-01-02  13:30:00  2021-01-02 13:30:00
2  2021-01-03  17:45:00  2021-01-03 17:45:00
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-16 12:21:55 +0000

Seen: 9 times

Last updated: Jun 16 '23