I have the following dataframe:
data={'Process_ID':['12345-98', '23547-75', '85763-99','44231-56','78456-00','53218-87'],
'Date': ['2021-06-30','2022-08-10','2021-06-15','2023-10-02','2024-04-03','2021-06-25'],
'Check': ['True','False','False','True','True','False']}
df=pd.DataFrame(data)
print(df)
The output is the following:
Process_ID Date Check
0 12345-98 2021-06-30 True
1 23547-75 2022-08-10 False
2 85763-99 2021-06-15 False
3 44231-56 2023-10-02 True
4 78456-00 2024-04-03 True
5 53218-87 2021-06-25 False
I wanted to select the process ids and the due dates only for the rows where check="True", so I did this:
def printfunc():
df['Check']=pd.eval(df['Check'].astype(str).str.title())
out=df.loc[df['Check'],['Process_ID','Date']].T
for x in out:
print('Process ID:',out[x].values[0],'\nDue Date:',out[x].values[1],'\n')
content=printfunc()
content
Output:
Process ID: 12345-98
Due Date: 2021-06-30
Process ID: 44231-56
Due Date: 2023-10-02
Process ID: 78456-00
Due Date: 2024-04-03
Now, I want to include the 'content variable' inside an f-string, because I will automate an email to display this information. However, when I try to do it, it returns a 'None' value:
email_text=f"""
Dear,
The due dates for the processes are the following:
{content}
Thank you.
Best Regards,
"""
print(email_text)
Output:
Dear,
The due dates for the processes are the following:
None
Thank you.
Best Regards,
How can I include this variable inside an f-string to be able to print it?
Try:
def printfunc():
s=''
df['Check']=pd.eval(df['Check'].astype(str).str.title())
out=df.loc[df['Check'],['Process_ID','Date']].T
for x in out:
s+='Process ID:'+out[x].values[0]+'\nDue Date: '+out[x].values[1]+'\n\n'
return s
content=printfunc()
Finally:
email_text=f"""
Dear,
The due dates for the processes are the following:
{content}
Thank you.
Best Regards,
"""
print(email_text)
Explainations:
this function is just printing the values it's not returning anything so that's the reason that you are getting 'None'
so we created a variable s
and assign empty character ''
to it and then adding and assigning the string back to it inside the for loop in the function
P.S: sorry for bad explaination...I am not good in explaining things :(