Pandas Split Mixed Type And Get First Element

Dance Party

Given the following data frame, which came from importing a messy Excel spreadsheet:

import pandas as pd
df=pd.DataFrame({'A':['a','b','c'],
        'dates':['2015-08-31 00:00:00','2015-08-24 00:00:00','8/3/2015, 1/4/16']})

try:
    df['dates']=df['dates'].astype('datetime64[ns]')
except:
    pass
df

    A   dates
0   a   2015-08-31 00:00:00
1   b   2015-08-24 00:00:00
2   c   8/3/2015, 1/4/16

I want to split where more than one date exists and take only the first one like this:

    A   dates
0   a   2015-08-31 00:00:00
1   b   2015-08-24 00:00:00
2   c   8/3/2015

I'm hoping it will convert the result to the same format like this:

    A   dates
0   a   2015-08-31 00:00:00
1   b   2015-08-24 00:00:00
2   c   2015-08-03 00:00:00

Thanks in advance!

MaxU

you can use to_datetime() in conjunction with .str.split():

In [215]: pd.to_datetime(df.dates.str.split(',\s*').str[0])
Out[215]:
0   2015-08-31
1   2015-08-24
2   2015-08-03
Name: dates, dtype: datetime64[ns]

or

In [216]: df['dates'] = pd.to_datetime(df.dates.str.split(',\s*').str[0])

In [217]: df
Out[217]:
   A      dates
0  a 2015-08-31
1  b 2015-08-24
2  c 2015-08-03

dtypes:

In [219]: df.dtypes
Out[219]:
A                object
dates    datetime64[ns]
dtype: object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Get the first element of an array

From Java

Get first item from Split()

From Dev

get first and last element in array

From Dev

Java split() is returning an empty first element

From Dev

Get first AND last element with SQLAlchemy

From Dev

swift: how to split a string and remove first element?

From Dev

Get First Element of JsonConvert results?

From Dev

How do I split the getActionCommand() in order to get the first element in the string?

From Dev

Split a string with powershell to get the first and last element

From Dev

Split a list by the type of element

From Dev

TreeMap get first element and remove

From Dev

Get the first element of a Mongo cursor

From Dev

Pandas read_table reads mixed type reading string as hexadecimal

From Dev

Pandas Mixed Type to Integer

From Dev

Split string and get last element

From Dev

Pandas Get First Element of Each Tuple in Cell

From Dev

Get first element with condition in Angular

From Dev

Simplehtmldom get first element

From Dev

Dojo get element type

From Dev

How to split the first and the last element on a Ruby string?

From Dev

Java split() is returning an empty first element

From Dev

Pandas: Difference with first element of the group

From Dev

Split string and get the first part with IF

From Dev

get first element after a space

From Dev

jQuery get first children of element

From Dev

Pandas read_table reads mixed type reading string as hexadecimal

From Dev

How do I get the first element of a dynamic/generic type

From Dev

Doctrine ODM Get By Id with mixed type

From Dev

Get first value that is not false from json mixed types