0

I have a data frame that includes the following values:

 Date   07/12/17
 Start Time   05:00 PM

Each of these is currently stored as strings.

I need to format/concatenate this information to be in the following format:

'2017-07-12 17:00'

I'm new to python and most of the solutions I've found don't apply to pandas data frames.

Example code:

#import pandas
import pandas as pd


#import file
df = pd.read_csv('Registrant List.csv', 
                 index_col='Conf Number', names = ['Conf Number','Email Address',
                 'Customer ID', 'First Name',   'Last Name', 'Sessions Date', 
                 'Session Time'])

#parse session time
df['Session Start Time'] = df['Session Time'].str.rpartition('-')[0]
print(df.to_string())

Example output:

Conf Number Email Address  Customer ID  First Name  Last Name  Sessions Date             Session Time Session Start Time                  
25502163     abc@123.com          NaN      Justin   Anderson      7/11/2017  10:30 AM - 11:00 AM EST          10:30 AM 
kiki1113
  • 47
  • 1
  • 8
  • Possible duplicate of [Convert DataFrame column type from string to datetime](https://stackoverflow.com/questions/17134716/convert-dataframe-column-type-from-string-to-datetime) –  Nov 29 '17 at 21:25
  • Could you provide an example of a dataframe to be formatted? As it stands, it is impossible to tell the structure of it. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – fuglede Nov 29 '17 at 21:28

1 Answers1

0

Try this. First you need to convert Start Time 05:00 PM into proper 24hour time format then concat the Date and Start Time columns as follows:

df_test = pd.DataFrame({'Date': ['07/12/17','08/12/17','08/12/17','09/12/17'],
                        'Start Time':['05:00 PM','01:30 PM','03:00 AM','04:50 PM']})

df_test

        Date  Start Time
0   07/12/17    05:00 PM
1   08/12/17    01:30 PM
2   08/12/17    03:00 AM
3   09/12/17    04:50 PM


df_test['Date_Start_Time'] =  pd.to_datetime(df_test.Date+' '+pd.to_datetime(df_test['Start Time']).dt.strftime('%H:%M'))

df_test
        Date  Start Time        Date_Start_Time
0   07/12/17    05:00 PM    2017-07-12 17:00:00
1   08/12/17    01:30 PM    2017-08-12 13:30:00
2   08/12/17    03:00 AM    2017-08-12 03:00:00
3   09/12/17    04:50 PM    2017-09-12 16:50:00

I think this is your desired output. Check it.

Space Impact
  • 13,085
  • 23
  • 48