简体   繁体   中英

Python Pivot or Crosstab using one column to calculate percentage difference

I'm trying to create a table that contains the percentage difference calculation for values in a column. I want to display this in a table, where the labels 0-15M, 15-25M...etc are on the left and top of the table and the values inside the table are the calculated percentage difference for each combination. The values I'm trying to use are below:

label   value
0-15M   18.274490   
15-25M  21.338270   
25-35M  22.607708   
35-45M  24.078338   
45-55M  25.545316   
55-65M  26.951005   
65-75M  27.765658

I have tried using pivot tables to do this, however trying to calculate using only one column doesn't seem to work. The code I've tried is below:

pd.pivot_table(test, index='label', columns='label', aggfunc=[np.mean])

And this is the result:

        0-15M   15-25M  25-35M  35-45M  45-55M  55-65M  65-75M

0-15M   18.27   NaN     NaN     NaN     NaN     NaN     NaN
15-25M  NaN     21.33   NaN     NaN     NaN     NaN     NaN
25-35M  NaN     NaN     22.60   NaN     NaN     NaN     NaN
35-45M  NaN     NaN     NaN     24.07   NaN     NaN     NaN
45-55M  NaN     NaN     NaN     NaN     25.54   NaN     NaN
55-65M  NaN     NaN     NaN     NaN     NaN     26.95   NaN
65-75M  NaN     NaN     NaN     NaN     NaN     NaN     27.76
75-85M  NaN     NaN     NaN     NaN     NaN     NaN     NaN
85-95M  NaN     NaN     NaN     NaN     NaN     NaN     NaN

So you can see its only calculating the values it already has, and not calculating new values from the combinations.

Any help with this would be great! Thanks.

Solution: I ended up using a loop to do this and appended lists together to form dataframes.

for x in df.iloc[2,]:
    for y in df.iloc[2,]:
        solution.append((x-y)/y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM