0

I have a pd dataframe. When I call pd.values, the result is like:

np.array([
    [1, 2, [0, 0, 0], 3],
    [1, 2, [0, 0, 0], 3]
])

and I want it to look like this when calling pd.values:

np.array([
    [1, 2, 0, 0, 0, 3],
    [1, 2, 0, 0, 0, 3]
])

Please help me out.

Jared
  • 31
  • 4

1 Answers1

2

Assuming your dataframe is:

df = pd.DataFrame([
    [1, 2, [0, 0, 0], 3],
    [1, 2, [0, 0, 0], 3]
])

I'd use the insight from this post by @wim where I present the modified function below.

This flattens arbitrarily nested collections.

from collections import Iterable

def flatten(collection):
    for element in collection:
        if isinstance(element, Iterable) and not isinstance(element, str):
            yield from flatten(element)
        else:
            yield element

I can then use this to flatten each row of the dataframe:

pd.DataFrame([*map(list, map(flatten, df.values))])

   0  1  2  3  4  5
0  1  2  0  0  0  3
1  1  2  0  0  0  3
piRSquared
  • 285,575
  • 57
  • 475
  • 624