sklal
sklal

Reputation: 175

Looping values of a column in dataframe to a function in Python

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!

Upvotes: 1

Views: 62

Answers (1)

Grismar
Grismar

Reputation: 31319

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.

Upvotes: 1

Related Questions