Reputation: 193
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
Upvotes: 0
Views: 1141
Reputation: 1821
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]
Upvotes: 1