Home > Software design >  Calculate Mrr in python
Calculate Mrr in python

Time:12-04

I found this answer on this question on Stackoverflow : Calculate MRR in Python Pandas dataframe

But when I try to implement it with my dataframe it returns an error, details:

                 startdate         amount  months  
0   2021-11-03 10:32:31         166.0   12  
1   2021-11-03 10:02:37         155.0   5

here is the code:

df.resample('M').sum()
dfs = []
for date, values in df.iterrows():
    months, price = values
    dfs.append(
        pd.DataFrame(
            # Compute the price for each month, and repeat this value
            data={'price': [price / months] * months},
            # The index is a date range for the requested number of months
            index=pd.date_range(date, periods=months, freq='M')
        )
    )

The error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-116-89e50648a780> in <module>
      1 dfs = []
      2 for date, values in df.iterrows():
----> 3     months, price = values
      4     dfs.append(
      5         pd.DataFrame(

ValueError: too many values to unpack (expected 2)

I've tried changing the variables and so on and couldn't find a way to solve the error. I am working with 1000 rows of data.

CodePudding user response:

You skipped a step in the original solution.

df = df.set_index('startdate')

The original solution turns the date column into the index so there are only 2 columns left.

months, price = values

Without this step date is just a numerical index and values contains all 3 columns which can't be unpacked into only 2 variables(the error you get)

  • Related