0

I have a numpy.DataFrame say df with 3 columns, col_1, col_2, col_3. Data in col_1 are numpy.ndarray and looks like this: array([ 0.216, -0.290, 0.349])

How could I use np.hstack() to expand the DataFrame with columns consist of each data point in col_1?

i.e. Original DataFrame

                     col_1   col_2               col_3
------------------------------------------------------
0   [0.216, -0.290, 0.349]  NORMAL  N09_M07_F10_K001_1

Supposed DataFrame

                 col_3  col_2     0      1     2
------------------------------------------------
0   N09_M07_F10_K001_1 NORMAL 0.216 -0.290 0.349

I'd tried like this:

Supposed_DataFrame = pd.concat(
    [df[['label', 'filename']], 
     pd.DataFrame(np.hstack(df["signal"].values).T)
    ], 
    axis=1)

but the output was:

                 col_3  col_2     0
-----------------------------------
0   N09_M07_F10_K001_1 NORMAL 0.216

any simpler solutions will be appreciated 😊

1
  • look for some sort of pandas 'explode'. This is a pandas task, not a numpy one. Commented Mar 27, 2021 at 10:48

1 Answer 1

1

Check out this code:

import pandas as pd

dict_ = {
    'col_1': [[0.216, -0.290, 0.349]],
    'col_2': 'NORMAL',
    'col_3': 'N09_M07_F10_K001_1'
        }
df2 = pd.DataFrame(dict_)

supposed_DataFrame = pd.concat([df2[['col_3', 'col_2']], pd.DataFrame(df2['col_1'].to_list(), columns=[0,1,2])], axis=1)
print (supposed_DataFrame)

Method-2 : using basic steps:

row_1 = df2['col_1'][0]
for i in range(len(row_1)):
    df2[i] = row_1[i]

df2.drop('col_1', axis=1, inplace=True)

print(df2)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I used your Method-2 and no longer need np.hstack()

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.