0

On one hand we have this Pandas Dataframe:

import pandas as pd
df = {"EventName":{"0":"Event234","1":"Event235","2":"Event236"},"EventType":{"0":"TypeA","1":"TypeA","2":"TypeB"},"Data1":{"0":"Data266276","1":"Data266277","2":"Data266278"},"Data2":{"0":"Data432562","1":"Data432563","2":"Data432564"}}
df = pd.DataFrame(df)
print(df)

Returning this:

  EventName EventType       Data1       Data2
0  Event234     TypeA  Data266276  Data432562
1  Event235     TypeA  Data266277  Data432563
2  Event236     TypeB  Data266278  Data432564

and on the other we have the following function, which sends a post request:

import time
import requests

# Test post url calls
# https://stackoverflow.com/questions/5725430/http-test-server-that-accepts-get-post-calls
AlertURL = "http://httpbin.org/anything"

def alert(msg):
    nowdt = time.strftime("%m/%d/%Y %H:%M:%S")
    payload = {"msg": str(nowdt)+", "+msg}    
    response = requests.post(AlertURL, payload)      
    print(response.text)

alert(df.EventName+" "+df.EventType+" "+df.Data1+" "+df.Data2)

The last line is sending one (1) request with all dataframe rows.

And we want to send one request per dataframe row (three in this case), with a 2 secs delay between them.

How do we do it?

FrankC
  • 11,113
  • 4
  • 16
  • 21
  • 1
    Can't you iterate through the rows using `df.iterrows()` or `df.itertuples()` and for delay use`time.sleep(2)`. Does it help? – mad_ Aug 27 '18 at 12:57

3 Answers3

2

use a for loop with the shape of your dataframe to loop through each row. Use i to specify which row you want to send. Finally use time.sleep to wait.

for i in range(df.shape[0]):
    alert(df.EventName[i]+" "+df.EventType[i]+" "+df.Data1[i]+" "+df.Data2[i])
    time.sleep(2)
VegardKT
  • 1,226
  • 10
  • 21
  • Thanks. This is working better with ifttt (the real post url I'm using) than the m.rp version with iterrows, even without the delay. – FrankC Aug 27 '18 at 13:47
2

You can use iterrows() ti iterate over a dataframe and time.sleep() to wait some time in a more or less deterministic way:

for index, row in df.iterrows(): 
  alert(row['EventName']+" "+row['EventType']+" "+row['Data1']+" "+row['Data2'])
  time.sleep(2) #2 seconds

If you prefer you can move the sleep instruction inside your alert function near the request, for clarity.

m.rp
  • 718
  • 8
  • 22
0
for row in df.iterrows():
    alert(row[0] + " " + row[1] + " " + row[2] + " " + row[3])
    time.sleep(2)
Neil
  • 3,020
  • 4
  • 25
  • 48
  • 2
    While this code may work in the specific situaation, an explanation text is typically necessary to illustrate why and how it works, which parts are significant and any implications and side effects. – ivan_pozdeev Aug 27 '18 at 16:03