Home > Back-end >  Seperating Date/Time column into two: Date and Time
Seperating Date/Time column into two: Date and Time

Time:03-16

I have a data frame that has a heading that looks like this. I want to use the string split method to create two separate columns: one for date and one for time.

          Date/Time      Lat      Lon    Base
0  7/1/2014 0:03:00  40.7586 -73.9706  B02512
1  7/1/2014 0:05:00  40.7605 -73.9994  B02512
2  7/1/2014 0:06:00  40.7320 -73.9999  B02512
3  7/1/2014 0:09:00  40.7635 -73.9793  B02512
4  7/1/2014 0:20:00  40.7204 -74.0047  B02512

So something like this:

     Date   Time      Lat      Lon    Base
0  7/1/2014 0:03:00  40.7586 -73.9706  B02512
1  7/1/2014 0:05:00  40.7605 -73.9994  B02512
2  7/1/2014 0:06:00  40.7320 -73.9999  B02512
3  7/1/2014 0:09:00  40.7635 -73.9793  B02512
4  7/1/2014 0:20:00  40.7204 -74.0047  B02512

CodePudding user response:

You can use:

out = pd.concat([df.pop('Date/Time').str.extract('(?P<Date>[^\s]*)\s(?P<Time>.*)'),
                 df], axis=1)
print(out)

# Output
       Date     Time      Lat      Lon    Base
0  7/1/2014  0:03:00  40.7586 -73.9706  B02512
1  7/1/2014  0:05:00  40.7605 -73.9994  B02512
2  7/1/2014  0:06:00  40.7320 -73.9999  B02512
3  7/1/2014  0:09:00  40.7635 -73.9793  B02512
4  7/1/2014  0:20:00  40.7204 -74.0047  B02512

CodePudding user response:

Here is what I have done: import pandas as pd

date, time = [],[]
for i in df['Date/Time']:
    to_append = i.split(' ') #Separates date and time for example: [date,time]
    date.append(to_append[0]) #Appending the dates
    time.append(to_append[1]) #Appending the times
df.pop('Date/Time') #Removing the old Date/Time
df.insert(0,'Date',date) #Creating Date Column at Start
df.insert(1,'Time',time) #Creating Time Column at Index 1
print(df)

Output:        Date     Time      Lat       Lon    Base
        0  7/1/2014  0:03:00  40.7586  -73.9706  B02512
        1  7/1/2014  0:05:00  40.7605  -73.9994  B02512
        2  7/1/2014  0:06:00  40.7320  -73.9999  B02512
        3  7/1/2014  0:09:00  40.7635  -73.9793  B02512
        4  7/1/2014  0:20:00  40.7204  -74.0047  B02512

CodePudding user response:

Just use split

df['date'] = df['date'].apply(lambda x : x.split(' ')[0])
df['Time'] = df['date'].apply(lambda x : x.split(' ')[1))

CodePudding user response:

you can either parse the date time string an split on the space or you can convert the column to date time and get the date component or the time component

data="""Index,Date/Time,Lat,Lon,Base
0,7/1/2014 0:03:00,40.7586,-73.9706,B02512
1,7/1/2014 0:05:00,40.7605,-73.9994,B02512
2,7/1/2014 0:06:00,40.7320,-73.9999,B02512
3,7/1/2014 0:09:00,40.7635,-73.9793,B02512
4,7/1/2014 0:20:00,40.7204,-74.0047,B02512"""

df=pd.read_csv(StringIO(data),sep=',')

print(df['Date/Time'])
df['Date']=df['Date/Time'].apply(lambda x: x.split(' ')[0])
df['Time']=df['Date/Time'].apply(lambda x: x.split(' ')[1])

df['Date/Time']=pd.to_datetime(df['Date/Time'])
df['Date']=df['Date/Time'].dt.date
df['Time']=df['Date/Time'].dt.time

print(df['Time'])
  • Related