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

3
  • sorry, I meant I used .values but that does not work. I will update question Commented Aug 7, 2017 at 2:10
  • what is your expected out put ? Commented Aug 7, 2017 at 2:11
  • expected output should be an array of values and not array of lists Commented Aug 7, 2017 at 2:11

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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])

Comments

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

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.