How to separate the year from a date with Python and Pandas?

Asked

Viewed 3,879 times

0

I have a student database with a Ticket Date column in the dd/mm/aa format. I need to generate an Ano_ticket column only with the year of the date of each record.

Exemplo da base

Resultado esperado

import pandas as pd
df = pd.read_csv('alunos.csv', sep='|')
data = df['DT_INGRESSO']
data.str.split('/')
ano = data[2]

I tried something with str.split(), which separates day, month and year into a list, but I couldn’t think of a way to do that for all the records. I’m a beginner in Python.

  • I believe you did not understand how pandas works, https://paulovasconcellos.com.br/28-commanderss-%C3%Pandas-pouches-that-maybe-you%C3%AA-n%C3%A3o-conhe%C3%A7a-6ab64beefa93

  • I’m a Python beginner in general. Any help is welcome

2 answers

1


First step to the column DT_INGRESSO for type datetime (and no longer string). Note that dates are in the format dd/mm/aaaa, then its shape is %d/%m/%Y:

df['DT_INGRESSO'] = pd.to_datetime(df['DT_INGRESSO'], format='%d/%m/%Y')

Then I take the attribute year (year) of each date and stored in a new column ANO:

df['ANO'] = df['DT_INGRESSO'].dt.year

  • It worked by converting to datetime. Thank you

0

Look I didn’t get to use pandas yet, but in its variable data is a list with column data. It has an easy way to get only the dates' years.

import pandas as pd
df = pd.read_csv('alunos.csv', sep='|')
data = df['DT_INGRESSO'] # se o resultado for uma lista das datas do banco
ano = [a.split('/')[-1] for a in data]
df['ANO_INGRESSO'] = ano

Browser other questions tagged

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