Reputation: 205
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
Upvotes: 0
Views: 173
Reputation: 205
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)
Upvotes: 0
Reputation: 1939
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
Now, 'X' can have any number of elements. I used 'X' with 4 elements for example.
Upvotes: 2