简体   繁体   中英

pandas: Divide DataFrame last row by first row

This questions is similar to Python: Pandas Divide DataFrame by first row

I have a DataFrame which looks like this:

            1125400  5430095  1095751
2013-04-02    98.91      NaN  5626.79
2013-04-03    99.29      NaN  5727.53
2013-04-04    99.79      NaN  5643.75
2013-04-07   100.55      NaN  5630.78
2013-04-08   100.65      NaN  5633.77

I would like to divide the values of the last row by the values of the first row in order to obtain the percentage difference over time.

一个更清晰的方法是使用iloc

df.iloc[0] / df.iloc[-1]

只需取第一行和最后一行的值,然后将它们df.T[df.index[0]] / df.T[df.index[-1]]即可,如下所示: df.T[df.index[0]] / df.T[df.index[-1]]

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