0

I have ended up with a pandas dataframe which looks like this:

               adm1    per_area_adm1
0          campeche  [2.57978273338]
1           chiapas  [10.9970300459]

Is there a way to get the values from per_area_adm1 column? I can do df['per_area_adm1'].values but that provides an array of lists

user308827
  • 21,227
  • 87
  • 254
  • 417

3 Answers3

3

If each list contains only one element, you can use .str[0]:

df.per_area_adm1.str[0]

#0     2.579783
#1    10.997030
#Name: per_area_adm1, dtype: float64
Psidom
  • 209,562
  • 33
  • 339
  • 356
1

Alternatively, df.apply(operator.itemgetter):

In [1026]: import operator

In [1027]: df.per_area_adm1.apply(operator.itemgetter(0)).values
Out[1027]: array([  2.57978273,  10.99703005])
cs95
  • 379,657
  • 97
  • 704
  • 746
0

you can use this function to reduce the list of lists to just a list

flat_list = [item for sublist in df['per_area_adm1'].values for item in sublist]
print(flat_list)

Reference: Making a flat list out of list of lists in Python

Naren Murali
  • 19,250
  • 3
  • 27
  • 54