简体   繁体   中英

Python and Pandas - Transferring Excel formula to Pandas

At the moment, I'm trying to migrate my Weibull calculations from an excel macro to Python, and the tool I've been primarily using is Pandas. The formula I am currently having trouble converting from excel to Python is as follows:

Adjusted Rank = (Previous value in the adjusted rank column) * (Another column's value), but the first value in the adjusted rank column = 0

My brain is trying to copy and paste this methodology to pandas, but as you can imagine, it doesn't work that way:

DF[Adjusted Rank] = (Previous value in the adjusted rank column) * DF(Another Column), but the first value in the adjusted rank column = 0

In the end, I imagine the adjusted rank column will look like so:

                     Adjusted Rank
                           0
                       Some Number
                       Some Number
                       Some Number
                       etc.

I'm having some trouble puzzling out how to make each "cell" in the adjusted rank column refer to the previous value in the column In Pandas. Additionally, Is there a way to set only the first entry in the column equal to 0? Thanks all!

您可以使用shift乘以先前的值,并在开头添加零,这应该可以:

   df['new'] = df['adjusted_rank'].shift(period = 1, fill_value=0) * df['another_column']

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