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.. +

James Cook
  • 334
  • 4
  • 16
  • To create a dict comprehension from multiple sublists `date, hours`, just do `{"day": date[i], "shopCapacity": hours[i]} for i in len(date)`. – smci Sep 01 '21 at 01:50
  • Define `payload` properly in your example (e.g. a list of dicts, or a dataframe column), to make your code [mcve] so we can answer better. Currently you're just overwriting `payload`. – smci Sep 01 '21 at 01: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. – tdelaney Sep 01 '21 at 01: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. – smci Sep 01 '21 at 02:12
  • That supposed dup was for lists. The accepted answer here shows how to use zip with dataframes. – tdelaney Sep 01 '21 at 02:18
  • 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. – tdelaney Sep 01 '21 at 02:21
  • @tdelaney: I've been pointing out that defining `payload` then immediately overwriting it isn't reproducible (MCVE) code, so you can't run a loop over it. That's all. Possible solutions include zip, dict comprehensions, or pandas `df.apply()`. – smci Sep 01 '21 at 02:40
  • @smci - the part under _I'm currently trying to use a Python loop._ is just meant to be descriptive of the intent. The next part, just under `Using...` is the code. Maybe OP should just take that bit out, I guess. But the actual code example reproduces the problem. – tdelaney Sep 01 '21 at 02:48

1 Answers1

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'}
U13-Forward
  • 69,221
  • 14
  • 89
  • 114