1

Is there any easy way to extract a data frame that has both timestamp and location in the same row?

1 of the columns in this dataframe:

event
2019-01-01 11:08:33.000 USA
2019-01-02 11:08:33.000 Mexico
2019-01-03 11:08:33.000 Canada

Any easy way to separate them into 2 columns e.g. time and location?

time,location
2019-01-01 11:08:33.000,USA
2019-01-02 11:08:33.000,Mexico
2019-01-03 11:08:33.000,Canada
Chubaka
  • 2,933
  • 7
  • 43
  • 58
  • Does this answer your question? https://stackoverflow.com/questions/14745022/how-to-split-a-column-into-two-columns – sshashank124 Jan 11 '20 at 09:31

1 Answers1

1

Use Series.str.rsplit if no space in location strings:

df[['time', 'loc']] = df.pop('event').str.rsplit(n=1, expand=True)
print (df)
                      time     loc
0  2019-01-01 11:08:33.000     USA
1  2019-01-02 11:08:33.000  Mexico
2  2019-01-03 11:08:33.000  Canada

Or regex solution with extract timespams with Series.str.extract:

regex = r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}) (.*)'
df[['time', 'loc']] = df.pop('event').str.extract(regex)
print (df)
                      time     loc
0  2019-01-01 11:08:33.000     USA
1  2019-01-02 11:08:33.000  Mexico
2  2019-01-03 11:08:33.000  Canada
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    the first solution works perfectly! Learning a new syntax pop and rsplit! Thank you! @jezrael – Chubaka Jan 11 '20 at 21:01