Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get the index 2-4 of a String in each entry of a column?

I have a column in a dataframe and need for every entry just the index 2-4 of the string.
For example

A
AB101CD
AB101CD
AB101CD

result:

A
101
101
101

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use df.column.apply

import pandas as pd    
a = ['AB101CD','AB101CD','AB101CD']
df = pd.DataFrame(a)
df.columns = ['A']
df.A.apply(lambda x:x[2:5])

This gives:

0    101
1    101
2    101
Name: A, dtype: object

You could set it as column:

 df.A = df.A.apply(lambda x:x[2:5])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading