Home > Net >  how to delete the two last characters of all values in a column in a dataframe in pandas python?
how to delete the two last characters of all values in a column in a dataframe in pandas python?

Time:12-12

I Have the following dataframe

Datetime

0 2022-06-01 00:00:00.0 1 2022-06-01 00:01:00.0 2 2022-06-01 00:02:00.0

i want to remove the last two characters so my dataframe will be

Datetime

0 2022-06-01 00:00:00 1 2022-06-01 00:01:00 2 2022-06-01 00:02:00

i test the following code but unless the fact that no error happens , nothing changed in my dataframe

data["Datetime"].str[:-2]

does anybody knows what i have to correct?

CodePudding user response:

Use iloc to drop last column of pandas dataframe .

CodePudding user response:

You can use:

data['Datetime'] = data['Datetime'].astype('datetime64[s]')

CodePudding user response:

You can specify the date and time format you want, like this (assuming the column is of datetime format)

format_string = '%Y-%m-%d %H:%M:%S'
# and then convert
df['column'] = df['column'].dt.strftime(format_string)

CodePudding user response:

I think you need assign back converted values to datetimes:

data["Datetime"] = pd.to_datetime(data["Datetime"])
  • Related