1

I have a pandas DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([['2021-09-01', 88], ['2021-09-02', 55]]),
                   columns=['Date', 'Hours'])

I want to construct multiple JSON payloads to call an external API based on the Dataframe values. I'm currently trying to use a Python loop.

    payload = {
        "day": '2021-09-01', 
        "shopCapacity": 88 
        }

    payload = {
        "day": '2021-09-02', 
        "shopCapacity": 55
        }

Using..

for date in df['Date']:
   for hours in df['Hours']:

      payload = {"day": date, 
                 "shopCapacity": hours
                 }
      print(payload)

Gives me..

{'day': '2021-09-01', 'shopCapacity': '88'}
{'day': '2021-09-01', 'shopCapacity': '55'}
{'day': '2021-09-02', 'shopCapacity': '88'}
{'day': '2021-09-02', 'shopCapacity': '55'}

I want...

{'day': '2021-09-01', 'shopCapacity': '88'}
{'day': '2021-09-02', 'shopCapacity': '55'}

I understand the nested loop is giving me the undesired output but I can't find a way to get the values I need.. +

8
  • To create a dict comprehension from multiple sublists date, hours, just do {"day": date[i], "shopCapacity": hours[i]} for i in len(date). Commented Sep 1, 2021 at 1:50
  • Define payload properly in your example (e.g. a list of dicts, or a dataframe column), to make your code minimal reproducible example so we can answer better. Currently you're just overwriting payload. Commented Sep 1, 2021 at 1:51
  • @smci - OP prints payload and then goes for the next. The desired and current output are generated from the posted code. I don't see a need to change. Commented Sep 1, 2021 at 1:55
  • @tdelaney: the OP says they want to construct multiple JSON payloads to call an external API. Printing is just a standin for the actual API call. If constructing the payload is complicated, they may want to save that as a dataframe column. They haven't said. Either way, is's better for the code here to be fully reproducible. Commented Sep 1, 2021 at 2:12
  • 1
    @smci - but that's what I was trying to say. The code (once you fix the wonky indentation) generates the output posted. print is a standin for some other call... and that's great! This is a minimal example, the gold standard. Commented Sep 1, 2021 at 2:21

1 Answer 1

2

Try to_dict with orient='records':

>>> df.to_dict('records')
[{'Date': '2021-09-01', 'Hours': '88'}, {'Date': '2021-09-02', 'Hours': '55'}]
>>> 

The reason your code doesn't work is because you're not zipping the values, instead iterating them to create combinations, so for your code try:

for date, hours in zip(df['Date'], df['Hours']):    
    payload = {"day": date, 
               "shopCapacity": hours}
    print(payload)

Output:

{'day': '2021-09-01', 'shopCapacity': '88'}
{'day': '2021-09-02', 'shopCapacity': '55'}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.