Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate Mrr in python

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading