Home > OS >  How to calculate value increase between an index and consecutive columns
How to calculate value increase between an index and consecutive columns

Time:01-20

My data frame has 6 columns and multiple rows. I would like to calculate how much the values in a row have increased or decreased (in percents) in relation to the index.

DataFrame such as::

a = np.random.randint(0,70, (10,7))
df = pd.DataFrame(np.random.randint(0,70, (10,7)), columns=['A', 'B', 'C', 'D', 'E', 'F', 'G'])
df = df.set_index(['C'])

CodePudding user response:

something like this?

(df/df.index.values.reshape(-1,1)-1)*100

output

  C          A         B          D         E          F         G
---  ---------  --------  ---------  --------  ---------  --------
 30  100        -53.3333   33.3333    53.3333   66.6667   106.667
 51  -94.1176     0       -23.5294   -70.5882    3.92157  -43.1373
 32   93.75     -31.25     93.75      59.375   -12.5       34.375
 63   -6.34921  -60.3175   -1.5873   -11.1111  -73.0159     3.1746
 52    3.84615  -28.8462   -9.61538   23.0769  -15.3846   -11.5385
 41    4.87805  -36.5854  -21.9512    14.6341   36.5854   -82.9268
 30  -33.3333   -20        30        -46.6667  -80        -20
 61  -78.6885   -13.1148  -31.1475   -31.1475  -49.1803   -21.3115
  7  700          0       700        785.714   200        871.429
 16   75         37.5     -12.5       56.25    112.5      143.75
  •  Tags:  
  • Related