0

What happens is that I have this set of data and in the column named Fecha de diagnóstico there are many terms like 00:00:00 that I want to eliminate, do you know any method with which I can eliminate all those terms?

Don't worry, don't pay attention to the rest, it's in Spanish.

I tried this

ruta.drop(ruta.loc[ruta['Fecha de diagnóstico']=='00:00:00'].index, inplace=True)

but it doesn't seem to work

this is i have

%matplotlib inline 
import matplotlib.pyplot as plt 
import numpy as np
import pandas as pd

ruta = pd.read_csv('/content/drive/MyDrive/datos punto 6/Casos_positivos_de_Covid-19_en_el_departamento_de_Antioquia.csv')
cambio = ruta.drop(['fecha reporte web','Fecha de notificación','Código DIVIPOLA departamento','Nombre departamento','Código DIVIPOLA municipio','Unidad de medida de edad','Código ISO del país','Nombre del país','Recuperado','Fecha de inicio de síntomas','Fecha de muerte','Fecha de recuperación','Tipo de recuperación','Pertenencia étnica','Nombre del grupo étnico','Ubicación del caso','Estado','Tipo de contagio'], axis=1)
5
  • Is 'Fecha de diagnóstico' a column of date-times? Can you provide a sample dataframe? Commented Mar 25, 2022 at 21:55
  • yes, is a column of date-times Commented Mar 25, 2022 at 21:57
  • 1
    I suppose there could be a reason for just wanting the strings. Undeleted my answer. Commented Mar 25, 2022 at 22:10
  • By the way, you can select which columns to read in with read_csv. That way you don't have to bother with dropping them after the data is in memory. See the docs for details on how to use the usecols parameter. Commented Mar 25, 2022 at 22:11
  • OP, if your question has been answered, please mark one as accepted. That way your question will be removed from the unanswered queue. Commented Mar 26, 2022 at 17:59

2 Answers 2

1

If you really have datetime type, keep only the date while keeping the type using:

df['col'] = df['col'].dt.date
Sign up to request clarification or add additional context in comments.

1 Comment

This will return a datetime.date, so @samglass if you want it a string you might need to do df['col'] = df['col'].dt.date.astype(str)
1

If you don't need to preserve the type, and simply want the date as a string, you can format the datetimes using .dt.strftime():

ruta['Fecha de diagnóstico'].dt.strftime("%Y-%M-%d")

The benefit here is that you can change the format of your date to however you want (i.e., / delimiters, or different order of day, month, and year).

The tradeoff is that you lose the datetime type, which you may want for some type of processing. If you want to preserve the datetime type of that column, use Mozway's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.