【问题标题】:How to calculate value increase between an index and consecutive columns如何计算索引和连续列之间的值增加
【发布时间】:2022-01-19 16:05:10
【问题描述】:

我的数据框有 6 列和多行。我想计算一行中的值相对于索引增加或减少了多少(以百分比为单位)。

DataFrame 如::

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'])

【问题讨论】:

  • 预期输出是多少?到目前为止,您尝试过什么?

标签: python pandas dataframe


【解决方案1】:

这样的?

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

输出

  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    相关资源
    最近更新 更多