How do I delete part of a string in a pandas.Dataframe

Asked

Viewed 98 times

0

I have a Dataframe with the column Assinatura.Cadastro, but the date and time is all together.

inserir a descrição da imagem aqui

How can I leave only the hours field, excluding the date ?

  • Thiago, I edited your question to facilitate the search for others with the same problem, if you find that I changed the direction a lot you can [Edit] or if you want you can return revisions

  • Perfect, no problem.

1 answer

0


As the data seems to me a string, Voce can use the method slice

df['hora'] = df['coluna'].str.slice(10)
# ou usando índices ↓
# df['hora'] = df['coluna'].str[10]

df['data'] = df['coluna'].str.slice(0,10)
# ou usando índices ↓
# df['data'] = df['coluna'].str[0:10]

See the working code on ideone

How do you want to replace you can just put:

df['Assinatura.Cadastro'] = df['Assinatura.Cadastro'].str.slice(0,10)
# ou usando índices ↓
# df['Assinatura.Cadastro'] = df['Assinatura.Cadastro'].str[0:10]
  • If it’s not a problem you can turn into String, df['coluna'].astype(str).str.slice(0,10) the .astype(str) turns into string

  • 1

    now I got, thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.