0

I have a unstructured Data frame in python which has two variables X and Y. Every observation in X is array and Y is class variable which looks like

             X           Y
      1. [ [ 1,2] ]      a
      2. [ [ 2,3] ]      b

I want to Have it like

 1.   1    2     a
 2.   2    3     b 

I have tried option from numpy to data frame but not working 

2 Answers 2

2
import pandas as pd
df=pd.DataFrame({'X':[[[1,2]],[[3,4]]],'Y':['a','b']})

def expand(x):
     x=x['X'][0]
     return x
df['X1'],df['X2']=zip(*df.apply(expand,axis=1))
df=df.drop(['X'],axis=1)

Explanation: using zip() with apply(axis=1), we can generate 2 new columns using 'X'.

For many elements in 'X':

import pandas as pd
df=pd.DataFrame({'X':[[[1,2,3,4]],[[3,4,5,6]]],'Y':['a','b']})

def expand(x):
    new_columns=x['X'][0]
    return new_columns+[x['Y']]
df=pd.DataFrame(zip(*df.apply(expand,axis=1))).T

enter image description here

Now, 'X' can have any number of elements. I used 'X' with 4 elements for example.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank u mehul ..I have one more question what if instade of just two elements in array if we have 100 elements in each observation array of X
so like you wish to make 100 new columns?
I want to make new 100 columns
Thank u so much ...Mehul
0
import pandas as pd

# Create the initial DataFrame
data = {'X': [[[1, 2]], [[2, 3]]], 'Y': ['a', 'b']}
df = pd.DataFrame(data)

# Transform the DataFrame
df_transformed = pd.DataFrame(df['X'].apply(lambda x: x[0]).tolist(), columns=['X1', 'X2'])
df_transformed['Y'] = df['Y']

# Display the transformed DataFrame
print(df_transformed)

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.