Gonza
Gonza

Reputation: 177

I don't understand the correct operation of substring in Pandas

I am having difficulty performing a substring to a field in my dataframe, I cannot correctly extract the values I need, I have reviewed the documentation but I cannot see what I am doing wrong. I leave an example:

>> df['date']
20200131

I am trying to substring to get '2020' + '01' + '31' independently to apply a format as follows:

>>df_t = df_test['date'].astype(str).str[:4]  #Work OK
  2020
>>df_t = df_test['date'].astype(str).str[5:2]  #No work
  
  df_t = df_test['date'].astype(str).str[7:2]  #No work
  

I have also tried another method, but with the same result:

df_t = df_test['date'].astype(str).str.slice(5,2) #No work

What am I doing wrong?

From already thank you very much!!

I will listen to your answers,

Regards!

Upvotes: 0

Views: 34

Answers (2)

Maug
Maug

Reputation: 35

The syntax in [begin:end:step] begin is included, and end is excluded

As when you ask for [5,2] you ask for the part starting at the index 5 and ending at the index 2 with the default step of 1, that is empty.

You can get the right thing with [:4] then [4:6] and [6:]

Upvotes: 0

Fabricio Fonseca
Fabricio Fonseca

Reputation: 139

df_t = df_test['date'].astype(str).str[5:6]

df_t = df_test['date'].astype(str).str[7:8]

Upvotes: 1

Related Questions