Home > Blockchain >  transform dataFrame in python
transform dataFrame in python

Time:11-14

I have a some easy dataFrame with columns X(random float) and Y(nominative A,B,C)

X Y

1.01 A

-1.09 B

0.2 c

I want to transform it to dataFrame with columns

xA xB xC

1.01 -1.09 0.2

how could I do that ?

with minimum lines of code

CodePudding user response:

df1.melt('Y').assign(Y=lambda x:x['variable']   x['Y']).pivot_table('value', columns='Y')

output:

Y       XA   XB    XC
value   1.01 -1.09 0.20

CodePudding user response:

`df= pd.read_csv("https://stepic.org/media/attachments/course/524/s_anova_test.csv")



xA=[]
xB=[]
xC=[]
for i in range(0, len(df.x)):
    if df.y[i]=='A':
        xA.append(df.x[i])
    elif df.y[i]=='B':
        xB.append(df.x[i])
    else: xC.append(df.x[i])

df_t= pd.DataFrame({'xA':xA,'xB':xB, 'xC':xC})
display(df_t) '
  • Related