1

I'm trying to run to a loop (column in a dataframe) over a function which returns a list of dictionary and should store the result of each of the iteration in a separate list for each of the values in a column.

Example:

name id type
a     1 sol
b     2 sol
c     3 sol

I have tried the below code but I'm not getting any result

for id in df:
    name_id = []
    name_id.append(getAct(67,id,'pow','gen'))


Output
    [{'status': 'success', 'message': 'success', 'data': []}]


def getAct(client_id, plant_id, tag_name, type_name):
    api_endpoint = ''

    today = datetime.date.today()

    headers = {'token': get_token()['access_token'],
               'Content-Type': 'application/json'}

    params = {'client_id': str(client_id),
              'from-date': str(today),
              'to-date': str(today),
              'tag': str(tag_name),
              'type': str(type_name),
              'plant-id': str(plant_id)
              }

    r = requests.post(url=api_endpoint, headers=headers, params=params)

    return r.json()

An example of getAct function:

getAct(67,1,'pow','gen')

    [{'utc_time': '2020-05-01 14:51:54',
    'value': -0.02,
    'insert': '2020-05-01 20:31:16'},
   {'utc_time': '2020-05-01 14:52:51',
    'value': -0.02,
    'insert': '2020-05-01 20:31:21'}]

Expected Output

a_1 = [{'utc_time': '2020-05-01 14:51:54',
        'value': -0.02,
        'insert': '2020-05-01 20:31:16'},
       {'utc_time': '2020-05-01 14:52:51',
        'value': -0.02,
        'insert': '2020-05-01 20:31:21'}]
b_2 = [{'utc_time': '2020-05-01 14:51:54',
        'value': 4.02,
        'insert': '2020-05-01 20:31:16'},
       {'utc_time': '2020-05-01 14:52:51',
        'value': 4.02,
        'insert': '2020-05-01 20:31:21'}]
c_3 = [{'utc_time': '2020-05-01 14:51:54',
        'value': 6.9,
        'insert': '2020-05-01 20:31:16'},
       {'utc_time': '2020-05-01 14:52:51',
        'value': 7.3,
        'insert': '2020-05-01 20:31:21'}]

I'm new to Python. Please help!

2
  • It's unclear how the expected data relates to your input data in the DataFrame, other than both input and output having three elements. You can not (or should not) get variables named after data in your script, nor is that very useful. And where is getAct coming from? Commented May 25, 2020 at 5:00
  • They are related by id. Each of the list of dictionaries in the expected output corresponds to the id number which is being passed as argument in the function. getAct is coming from an API. I'm adding the function for your kind perusal and needful. Commented May 25, 2020 at 5:08

1 Answer 1

1

It appears this would do what you ask:

import pandas as pd

df = pd.DataFrame([
    {'name': 'a', 'id': 1, 'type': 'sol'},
    {'name': 'b', 'id': 2, 'type': 'sol'},
    {'name': 'c', 'id': 3, 'type': 'sol'}
])
# showing this is the same as the data you shared in the example
print(df.to_string(index=False))


def dummy_getAct(client_id, plant_id, tag_name, type_name):
    return [
        {'utc_time': 'some date', 'value': 1, 'insert': 'some date'},
        {'utc_time': 'some date', 'value': 2, 'insert': 'some date'}
    ]


result = {i: dummy_getAct(67, i, 'pow', 'gen') for i in df['id']}
print(result)

Result:

name  id type
   a   1  sol
   b   2  sol
   c   3  sol
{1: [
    {'utc_time': 'some date', 'value': 1, 'insert': 'some date'}, 
    {'utc_time': 'some date', 'value': 2, 'insert': 'some date'}], 
 2: [{'utc_time': 'some date', 'value': 1, 'insert': 'some date'}, 
     {'utc_time': 'some date', 'value': 2, 'insert': 'some date'}], 
 3: [{'utc_time': 'some date', 'value': 1, 'insert': 'some date'}, 
     {'utc_time': 'some date', 'value': 2, 'insert': 'some date'}]}

So, the key line is:

result = {i: dummy_getAct(67, i, 'pow', 'gen') for i in df['id']}

And you'd access a specific result with result[1]; it's just a dictionary with the id as keys.

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.