2

I'm trying to convert my dataframe columns to arrays. For example, I have a dataframe that looks like this:

Total  Price   Carrier
2      3       C
1      5       D

I'd like to convert the columns to arrays like this: [[2, 1], [3,5], ['C','D]] I do not want the column names.

I've tried doing this:

df["all"] = 1
df.groupby("all")[["Total","Price", "Carrier"]].apply(list)

However, I get something like this ["Total", "Price", "Carrier"] and is an object and not an array. How can I convert all columns to arrays?

0

1 Answer 1

4

Use df.values instead of apply:

>>> df.values.T.tolist()
[[2, 1], [3, 5], ['C', 'D']]
Sign up to request clarification or add additional context in comments.

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.