How to add another iterator to nested loop in python without additional loop?

2024/10/6 9:13:00

I am trying to add a date to my nested loop without creating another loop. End is my list of dates and end(len) is equal to len(year).

Alternatively I can add the date to the dataframe (data1) is that a better solution?

Data-Sample

state_list = ['A','B','C','D','E'] #possible statesdata1 = pd.DataFrame({"cust_id": ['x111','x112'], #customer data"state": ['B','E'],"amount": [1000,500],"year":[3,2],"group":[10,10],"loan_rate":[0.12,0.13]})data1['state'] = pd.Categorical(data1['state'], categories=state_list, ordered=True).codeslookup1 = pd.DataFrame({'year': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],'lim %': [0.1, 0.1, 0.1, 0.1, 0.1,0.1, 0.1, 0.1, 0.1, 0.1]}).set_index(['year'])matrix_data = np.arange(250).reshape(10,5,5) #3d matrix by state(A-E) and year(1-10)end = pd.Timestamp(year=2021, month=9, day=1)    # creating a list of dates
df = pd.DataFrame({"End": pd.date_range(end, periods=10, freq="M")})
df['End']=df['End'].dt.day
End=df.values

Calculation

results={}
for cust_id, state, amount, start, group, loan_rate in data1.itertuples(name=None, index=False):res = [amount * matrix_data[start-1, state, :]]for year in range(start+1, len(matrix_data)+1,):res.append(lookup1.loc[year].iat[0] * np.array(res[-1]))res.append(res[-1] * (loan_rate)) # *(End/365) # I want to iterate hereres.append(res[-1]+ 100)res.append(multi_dot([res[-1],matrix_data[year-1]]))results[cust_id] = res

example of expected output:

{'x111': [array([55000, 56000, 57000, 58000, 59000]),array([5500., 5600., 5700., 5800., 5900.]),array([56.055, 57.074, 58.093, 59.112., 60.132.]),

line 3 - calculation example ((5500 * 0.12) * (30/365))

array([5500., 5600., 5700., 5800., 5900.])- the entire line will be multiplied by loan_rate and (30/365)

Answer

If I understand correctly, the question boils down to this:

There is an

  • array of values, e.g.
vals = np.array([5500., 5600., 5700., 5800., 5900.])  # == res[-1] from the example
  • a constant loan rate, e.g.
loan_rate = 0.12  # ... to continue with the example above
  • and the goal is to perform a calculation ...
value * loan_rate * (end_date/365)

... for each value where the end_date comes from the array End (array([[30], [31], ..., [30]])).

  • So for example if year == 1:
    • 5500 * 0.12 * 30/365
    • 5600 * 0.12 * 30/365
    • 5700 * 0.12 * 30/365
    • ...
  • if year == 2: then the end_date is 31

Then you could just look up the end date depending on the year. Note the use of year - 1 as end_dates is 0 indexed and the counting of years seems to start with 1.

end_dates = End.reshape(-1)  # array([30, 31, 30, 31, 31, 28, 31, 30, 31, 30]); just to simplify access to the end date values
results={}
for cust_id, state, amount, start, group, loan_rate in data1.itertuples(name=None, index=False):res = [amount * matrix_data[start-1, state, :]]for year in range(start+1, len(matrix_data)+1,):res.append(lookup1.loc[year].iat[0] * np.array(res[-1]))res.append(res[-1] * loan_rate * end_dates[year-1]/365) # year - 1 hereres.append(res[-1]+ 100)res.append(np.linalg.multi_dot([res[-1],matrix_data[year-1]]))results[cust_id] = res  # no need to store the results of a customer multiple times; see comment below question
https://en.xdnf.cn/q/119510.html

Related Q&A

How to know where the arrow ends in matplotlib quiver

I have programmed plt.quiver(x,y,u,v,color), where there are arrows that start at x,y and the direction is determined by u,v. My question is how can I know exactly where the arrow ends?

how send text(sendkey) to ckeditor in selenium python scripting -chrome driver

I cant send a text to the text box of CKEditor while I scripting.it not shown in the seleniumIDE recording also.Help me to fix this issueASAP

How to replace the column of dataframe based on priority order?

I have a dataframe as follows df["Annotations"] missense_variant&splice_region_variant stop_gained&splice_region_variant splice_acceptor_variant&coding_sequence_variant&intron…

Scraping from web page and reformatting to a calender file

Im trying to scrape this site: http://stats.swehockey.se/ScheduleAndResults/Schedule/3940And Ive gotten as far (thanks to alecxe) as retrieving the date and teams.from scrapy.item import Item, Field fr…

Python Text to Data Frame with Specific Pattern

I am trying to convert a bunch of text files into a data frame using Pandas. Thanks to Stack Overflows amazing community, I almost got the desired output (OP: Python Text File to Data Frame with Specif…

Python Multiprocess OpenCV Webcam Get Request [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

error when trying to run my tensorflow code

This is a follow up question from my latest post: Put input in a tensorflow neural network I precoded a neural network using tensorflow with the MNIST dataset, and with the help of @FinnE was able to c…

ValueError: invalid literal for int() with base 10: Height (mm)

import csv from decimal import *def mean(data_set):return Decimal(sum(data_set)) / len(data_set)def variance(data_set):mean_res = mean(data_set)differences = []squared_res = []for elem in data_set:diff…

Having trouble in getting page source with beautifulsoup

I am trying to get the HTML source of a web page using beautifulsoup. import bs4 as bs import requests import urllib.request sourceUrl=https://www.pakwheels.com/forums/t/planing-a-trip-from-karachi-to-…

Cannot convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime

Im trying to convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime but the change is not saved: type(df_unix.loc[45,LastLoginDate])OUTPUT: pandas._libs.tslibs.timestamps.Timestampt…