python - Adding DataFrame groups as data columns -



python - Adding DataFrame groups as data columns -

i've been struggling find way reshape dataframe liking. i'm new python , not familiar methods of dataframe. particularly pivoting. i've read through docs multiple times , still haven't found solution.

(data below random)

my original info pulled dataframe looks this:

shellsurface s1 s2 elementhid sx sy sz sxy 0 1 88.340153 -88.340153 144 0 0 0 15.225413 1 1 66.370153 -66.370153 144 0 0 0 21.447455 2 1 74.422513 -74.422513 144 0 0 0 88.114254 3 1 22.324573 -22.324573 144 0 0 0 74.370153 4 2 14.322413 -14.322413 144 0 0 0 11.114425

there 3 surfaces per element, , elements quadrilaterals, have 4 separate entries need averaged in file. used frame.groupby(['elementhid','shellsurface']).mean() accomplish this.

the new frame:

s1 s2 sx sy sz sxy elementhid shellsurface 144 1 22.310153 -22.310153 0 0 0 21.445778 2 17.114552 -17.114552 0 0 0 11.114452 3 79.370153 -79.370153 0 0 0 19.311443

the problem need columns info in each surface (appending surface number headers) elements row. need column format below:

elementhid s11 s12 s13 sx1 sx2 sx3 sy1 sy2 sy3 sxy1 sxy2 sxy3

the way i've done making element class , giving attributes of info columns each surface, not seem efficient way it. i'm hoping there's way accomplish in pandas.

i've stacked data, gives me easy way loop through info each surface, i'm still not sure how utilize reshape data.

starting grouped data, index reset columns.

df = frame.groupby(['elementhid','shellsurface']).mean().reset_index()

you can utilize pivot_table reshape data. index defines variable defines 'rows' of table, , columns defines variable(s) should pivoted columns.

in [233]: pivoted = df.pivot_table(index=['elementhid'], columns=['shellsurface']) in [234]: pivoted out[234]: s1 s2 sx sy sz ... shellsurface 1 2 1 2 1 2 1 2 1 2 elementhid 144 62.864348 14.322413 -62.864348 -14.322413 0 0 0 0 0 0

the pivoted table have multiindex column, can access groups relatively intuitively, example:

in [235]: pivoted['s1'][1] out[235]: elementhid 144 62.864348 name: 1, dtype: float64

or, if want collapse downwards list of joined column names, this.

in [239]: pivoted.columns = [''.join((lvl1, str(lvl2))) lvl1,lvl2 in pivoted.columns] in [240]: pivoted out[240]: s11 s12 s21 s22 sx1 sx2 sy1 sy2 ... elementhid 144 62.864348 14.322413 -62.864348 -14.322413 0 0 0 0

python pandas

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -