0

I have a large dataframe df_c with a column type_info1 each rows contains a list with two elements .

I need to access the first element of each list on the array object. Is there a quick way to do it directly on the dataframe without going through the for loop etc or maybe use numpy :

df_c.loc[:,'type_info1'].values

results :

array([list(['AB#5', 'XYZ/ABCD']), list(['TB#5', 'XYZ/ABCD']),
       list(['CD#5', 'XYZ/ABCD']), ..., list(['BF#5', 'XYZ/ABCD']),
       list(['GH#7', 'XYZ/ABCD']), list(['FL#5', 'XYZ/ABCD'])],
      dtype=object)

any suggestion is welcome. thank you

2 Answers 2

1

Use an accessor: df_c['type_info1'].str[0]

Sign up to request clarification or add additional context in comments.

Comments

1

You could use list comprehension for this:

[item[0] for item in df_c.loc[:,'type_info1'].values]

or perhaps zip:

list(zip(*df_c.loc[:,'type_info1'].values))[0]

Both of these assume that none of the lists are empty btw.

If some are empty and you could e.g. skip those in the list comprehension:

[item[0] for item in df_c.loc[:,'type_info1'].values if item]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.