0

[SOLVED]

this worked for:

for lab, row in consig_df.iterrows():
     consig_df.loc[lab,"start_loan"] = d - dateutil.relativedelta.relativedelta(months=row["number_of_quotas_paid"])

[SOLVED]

I have the following df:

[edit]

       total_value           quotas_value    term            number_of_quotas_paid
0         3500.00            544.21           24                     24   
1        11226.17            648.55           60                     24   
2        11547.00            578.06           96                     18   
3         1500.00            418.99            4                      0   
4         5500.00           2928.49            2                      0   
5         7758.00            393.00           60                     12   
6         1000.00            532.84            2                      2   
7         2250.00            623.95            4                      0   
8          600.00            217.97            3                      3   
9          600.00            165.85            4                      2  

I want to run a loop, create a new row and append the data. I'm trying to calculate what month the loan started based on the number of quotas already paid.

I'm stuck after the loop.

import datetime
import dateutil.relativedelta

d = datetime.datetime.strptime("2018-08-01", "%Y-%m-%d")

for lab, row in consig_df.iterrows():

    comeco = d - dateutil.relativedelta.relativedelta(months=row["number_of_quotas_paid"])

How do I go from here, create a new row and append the data generated through the loop?

thanks

  • You probably don't want to use iterrows here... It also looks like you have a *column* and not a row... could you provide an example of your DF? You can use https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples for some hints on forming a solveable pandas question. – Jon Clements Dec 15 '18 at 19:23
  • You're right. I meant column. What do you suggest instead of iterrows? – Pedro Miranda Dec 15 '18 at 19:25
  • Nothing at the moment as no one has an idea of what your actual data looks like and thus what options may be available - but it's almost certainly not `iterrows`... :) – Jon Clements Dec 15 '18 at 19:26
  • just added an example. thanks for the patience . – Pedro Miranda Dec 15 '18 at 19:32
  • Thanks for taking the time to add the details :) – Jon Clements Dec 15 '18 at 19:33
  • Something like: `pd.to_datetime('2018-08-01') - pd.to_timedelta(df.number_of_quotas_paid, unit='M')` probably gets you close, but you probably want to specify how you want to anchor the results? – Jon Clements Dec 15 '18 at 19:39
  • When I run the code giving a specific value it works fine. d = datetime.datetime.strptime("2018-08-01", "%Y-%m-%d") comeco = d - dateutil.relativedelta.relativedelta(months=2) my problem is with the loop + append. – Pedro Miranda Dec 15 '18 at 19:51
  • Right... but you shouldn't really need an explicit loop... is the above close to what you're after? – Jon Clements Dec 15 '18 at 19:54
  • got it! thanks for your attention!! – Pedro Miranda Dec 15 '18 at 20:03

0 Answers0