0

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.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Lars
  • 15
  • 4
  • `months, price = values[["months", "amount"]]` because values is a dictionary with column names. EDIT: double square brackets – Larry the Llama Dec 04 '21 at 10:11
  • Note that the line `df.resample('M').sum()` doesn't result in anything, since you don't assign it to anything. It could be left out and the result of your code won't change. – 9769953 Dec 04 '21 at 10:12
  • 1
    Also, why are you dividing then multiplying by months - am I missing something? – Larry the Llama Dec 04 '21 at 10:12
  • @LarrytheLlama Technically, `values` is a Pandas row. – 9769953 Dec 04 '21 at 10:12
  • Is your intent to actually make `months` length lists in the new dataframe? Because that is what `[price / months] * months` does. – 9769953 Dec 04 '21 at 10:14
  • Oh @9769953 - that makes more sense – Larry the Llama Dec 04 '21 at 10:14
  • Actually, `months` length lists of `[price / months]` as a single cell for a column inside a dataframe (or generally) doesn't make any sense to me. There might be some broader context I'm missing, but alternatively, it could be an x-y problem: solving an underlying problem the difficult (wrong) way. – 9769953 Dec 04 '21 at 10:20

1 Answers1

0

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)

  • Yes I did the first step but I think I understand the error by your explanation and by other comments. – Lars Dec 04 '21 at 10:46