2

How can the array (the length of the array is constant for all elements in the series) be extracted into columns efficiently?

import pandas as pd
d = pd.DataFrame({'foo':\[1,2,3\], 'bar':\[\[1,1,1\], \[2,2,2\], \[3,3,3\]\]})
d][1]][1]

enter image description here

I.e. extract the array of [1,1,1] into a bar_0, bar_1, bar_3 column?

Is there a better way than manually iterating over the indices in the array and calling pandas.apply?

2 Answers 2

5
import pandas as pd

d = pd.DataFrame({"foo": [1, 2, 3], "bar": [[1, 1, 1], [2, 2, 2], [3, 3, 3]]})

d = pd.concat([d, d.pop("bar").apply(pd.Series).add_prefix("bar_")], axis=1)
print(d)

Prints:

   foo  bar_0  bar_1  bar_2
0    1      1      1      1
1    2      2      2      2
2    3      3      3      3
Sign up to request clarification or add additional context in comments.

1 Comment

Very convenient - but not super fast.
5

How about this:

>>> d.join(pd.DataFrame(d['bar'].to_list(), columns=['bar_1', 'bar_2', 'bar_3']))
   foo        bar  bar_1  bar_2  bar_3
0    1  [1, 1, 1]      1      1      1
1    2  [2, 2, 2]      2      2      2
2    3  [3, 3, 3]      3      3      3

You convert the bar column to list (nested list), convert it to a dataframe, and join the new dataframe with your initial dataframe.

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.