Recursively creating Data frame based on function output

General discussion for off-topic subjects.
maggie34
Junior Member
Posts: 1
Joined: Mon Aug 02, 2021 8:49 pm

Recursively creating Data frame based on function output

Postby maggie34 » Mon Aug 02, 2021 9:09 pm

I have column called Serial Number whose value can be in range from 28 - 34.I read and excel file and make the Serial number column as 32 then pass it to the function which has all the api information and return the dataset. Then I Create a new dataframe where length of the response = 1 and store the output. Then I pass the remaining values to function again with Serial number 34 characters and get the response back and save it to the output.

Here is my code

Code: Select all

try:
    data = pd.read_excel(filename, usecols=[4,18,19,20,26,27,28],converters={'Serial Number' :'{:0>32}'.format })
except Exception as e:
    sys.exit("Error reading %s: %s" % (filename, e))
 
data["Subject Organization"].fillna("N/A",inplace= True)
data = data[data['Subject Organization'].str.contains("XYX",case = False)]
print ("fetch Serial Number with 32 characters")
data["Output"] = data['Serial Number'].apply(lambda x: fetch_by_ser_no(x))
global _FETCHED
_FETCHED = 0
data["Serial Number 34"] = data['Serial Number'].str.zfill(34)
data.loc[:,"itemlen"] = data["Output"].str.len()
data['itemlen'] = data['itemlen'].astype(str)
df = data[data['itemlen'].str.contains("1")]
del df['Serial Number 34']
df = df[['Description','Certificate Issued','Certificate Expires','Subject Organization','Subject Common Name','Subject Alternate Names','Output','Serial Number','itemlen']]
data.drop(data[data['itemlen'] == "1" ].index,inplace = True)
print("Fetch Serial Number with 34 character start")
data['Output']  = data['Serial Number 34'].apply(lambda x: fetch_by_ser_no(x))
del data['Serial Number']
data.rename(columns ={'Serial Number 34':'Serial Number'},inplace = True)
data = data.append(df)

The Problem is I need to check it 4 times for 28,30,32,34 .

Is there a way I can create dynamic data frames using a for loop? I need to create a excel file at the end which shows all the output together.

Steps the code is doing -:

1.Read the excel file and create a dataframe "data" where serial number = 32 characters

2.Filter the Records from the file based on column Subject Organization = 'XYX'

3.Pass the Serial Number to a function (fetch by serial Number) and get the output in "Output" columns.

4.If the Output column has length = 1 then create a new dataframe "df" and save the output.But where "Output" column = 0 we need to make the Serial Number = 34 characters and pass it to function again to get the output.

5.Append both the data and df which will have same columns names and write the combined dataframe to an excel sheet.

Return to “Whatever”

Who is online

Users browsing this forum: No registered users and 12 guests