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 to get first value in a column in pandas dataframe

I have a pandas dataframe with single column and that column has multiple values in there. I want to get first value. Below is the example of dataframe with column name= A.
I want to get value abc, XYZ in my output.
how can I do that?

    A
abc, 123, 888
XYZ, 789, 999

>Solution :

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

Assuming the column A contains strings, use:

df['A'].str.split(', ').str[0]

Output (Series):

0    abc
1    XYZ
Name: A, dtype: object

Or using a list comprehension:

[e.split(', ')[0] for e in df['A']]

Output (list): ['abc', 'XYZ']


If you have lists:

df['A'].str[0]

Or:

[e[0] for e in df['A']]
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