Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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