Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Iterate through dataframe using f-strings?

Iterate through dataframe using f-strings

Imagine you have the following df:

d = {'KvK': [0.21, 0.13, 0.1], 'line amount#2': [0.0, 0.05, .05], 'ExclBTW': [0.5, 0.18, .05]}
df = pd.DataFrame(data=d)
df

    KvK line amount#2   ExclBTW
0   0.21    0.00         0.50
1   0.13    0.05         0.18
2   0.10    0.05         0.05

Now, I want to input each KvK value into an API call.

I've tried the following:

for index, row in df.iterrows():
    details = df(row)
    f"https://api.kvk.nl/api/v2/search/companies?user_key=ldbb&q=f{row['KvK']}"

However this does not work.

Desired output:

    KvK line amount#2   ExclBTW    APIoutput#1
0   0.21    0.00         0.50        0.21APIsampleAPIOutput
1   0.13    0.05         0.18        0.13APIsampleAPIOutput
2   0.10    0.05         0.05        0.10APIsampleAPIOutput

Please help!

like image 589
Max Avatar asked Dec 21 '25 15:12

Max


1 Answers

d = {'KvK': [0.21, 0.13, 0.1], 'line amount#2': [0.0, 0.05, .05], 'ExclBTW': [0.5, 0.18, .05]}
df = pd.DataFrame(data=d)

url = f"https://api.kvk.nl/api/v2/search/companies?user_key=ldbb&q=f"
df['APIoutput'] = url + df['KvK'].astype(str) 
like image 199
Dean Taler Avatar answered Dec 24 '25 05:12

Dean Taler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!