Monday, 15 September 2014

pandas - Python - Unpivot data -



pandas - Python - Unpivot data -

looking python solution. need assistance in unpivoting info frame in python. construction little funky basic pivot function how i'd reshape it.

current info frame - here's have

abc mechanical standard 15-day 10-day 5-day terminal units 0.49 0.75 0.69 0.63 diffusers 0.35 0.55 0.45 0.4 vent 0.8 0.95 0.9 0.85 piping 0.7 0.85 0.8 0.75 stoves 0.6 0.8 0.75 0.7

unpivoted info frame - here's how want reshape it

df.columns= customer, product category, ship cycle, multiplier df.index= abc mechanical client product category ship cycle multiplier abc mechanical terminal units standard 0.49 abc mechanical terminal units 15-day 0.75 abc mechanical terminal units 10-day 0.69 abc mechanical terminal units 5-day 0.63 abc mechanical diffusers standard 0.35 abc mechanical diffusers 15-day 0.55 abc mechanical diffusers 10-day 0.45 abc mechanical diffusers 5-day 0.4

any assistance much appreciated!

thanks!

if df looks this:

in [26]: df out[26]: standard 15-day 10-day 5-day terminal units 0.49 0.75 0.69 0.63 diffusers 0.35 0.55 0.45 0.40 vent 0.80 0.95 0.90 0.85 piping 0.70 0.85 0.80 0.75 stoves 0.60 0.80 0.75 0.70

then pd.melt gets close desired dataframe:

in [27]: pd.melt(df.reset_index(), id_vars=['index']).sort(['index']) out[27]: index variable value 1 diffusers standard 0.35 6 diffusers 15-day 0.55 11 diffusers 10-day 0.45 16 diffusers 5-day 0.40 3 piping standard 0.70 8 piping 15-day 0.85 13 piping 10-day 0.80 18 piping 5-day 0.75 4 stoves standard 0.60 9 stoves 15-day 0.80 14 stoves 10-day 0.75 19 stoves 5-day 0.70 0 terminal units standard 0.49 5 terminal units 15-day 0.75 10 terminal units 10-day 0.69 15 terminal units 5-day 0.63 2 vent standard 0.80 7 vent 15-day 0.95 12 vent 10-day 0.90 17 vent 5-day 0.85

i don't understand "abc mechanical" in original dataframe, haven't attempted include in result. column names can renamed this:

in [28]: df = pd.melt(df.reset_index(), id_vars=['index']).sort(['index']) in [29]: df.columns = ['product category', 'ship cycle', 'multiplier'] in [31]: df.head() out[31]: product category ship cycle multiplier 1 diffusers standard 0.35 6 diffusers 15-day 0.55 11 diffusers 10-day 0.45 16 diffusers 5-day 0.40 3 piping standard 0.70

python pandas

No comments:

Post a Comment