Home > OS >  How to get first value in a column in pandas dataframe
How to get first value in a column in pandas dataframe

Time:02-17

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

CodePudding user response:

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']]
  • Related