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,018
  • 70
  • 229
  • 377

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
  • 195,464
  • 25
  • 298
  • 322
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
  • 330,695
  • 80
  • 606
  • 657
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
  • 13,809
  • 3
  • 22
  • 48