从数据帧到函数传递不同类型的多个变量

时间:2019-03-16 13:05:16

标签: python pandas function dataframe

我有以下数据框:

import pandas as pd

data = {'selectionId': [8567238,7450487,12787737,9541421,10162696,7208966,8826166,7256678],

 'Price': [4.1,4.6,5.5,7.2,7.8,17.0,32.0,34.0],

 'Win_Percentage': [0.245870,0.212396,0.178922,0.145448,0.111974,0.078501,0.045027,0.011553],

  'Fit':[0.245870,0.212396,0.178922,0.145448,0.111974,0.078501,0.045027,0.011553],

   'size':[2.708701,2.373962,2.039223,1.704484,1.369744,1.035005,0.700266,0.365527]}

df = pd.DataFrame(data, columns=['selectionId', 'Price', 'Win_Percentage','Fit','size'])

我还具有以下功能:

def test(marketId, selectionId):

    global place_order_Req

    place_order_Req = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"' + marketId + '","instructions":'\


'[{"selectionId":"' + str(
        selectionId) + '","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1.9","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}'"""
print(place_order_Req)
"""

marketId变量始终具有值marketId = "1.156196315"

我想将selectionId中的df值传递给该函数。

我还想将size列的值传递给函数以更改函数的"size":"1.9"部分。

总而言之,我希望从函数中返回以下内容:

'{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"8567238","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"2.708701","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}\n    print(place_order_Req)\n    '

,并针对数据帧的每一行执行此操作。

为此,我尝试了以下方法:

selectionId = df['selectionId']

size = df['size'].astype(str)

def test(marketId, selectionId, size):

    global place_order_Req, place_order_Req_list, place_order_Req_size_list

    place_order_Req_list = []

    place_order_Req_size_list = []

    for i in selectionId:
        place_order_Req = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"' + marketId + '","instructions":'\
                                                                                                                              '[{"selectionId":"' + str(
            i) + '","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}'
        """
    print(place_order_Req)
    """
        place_order_Req_list.append(place_order_Req)

    for j in place_order_Req_list:

        place_order_Req = place_order_Req[:208] + j + place_order_Req[:209]

        place_order_Req_size_list.append(place_order_Req)

    print(place_order_Req_size_list)

这可以很好地更改selectionId变量,但是当我尝试更改"1.9"时它不起作用。对于向place_order_Req_size_list列表的每次输入,它也会返回两次。

我还认为必须有一种比使用两个循环更聪明的方法。

这是它返回的列表:

['{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"8567238","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"', '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7450487","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{', '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"12787737","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{', '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"9541421","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{', '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"10162696","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{', '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7208966","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{', '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"8826166","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{', '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"1","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"1.156196315","instructions":[{"selectionId":"7256678","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":{']

任何帮助都会很棒,欢呼。 桑迪

3 个答案:

答案 0 :(得分:2)

您可以仅将函数应用于数据框的每一行。您可以读取每一行的字段'selectionId''size',并将其传递给您的place_order_Req变量。另外,我不确定您是否真的需要将place_order_Req定义/使用为全局变量。

def test(x):    
    marketId = "1.156196315" #static value
    selectionId = x['selectionId']
    size = x['size']

#     global place_order_Req
    place_order_Req = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"' + marketId + '","instructions":' '[{"selectionId":"' + str(selectionId) + '","handicap":"0","side":"BACK","orderType":"LIMIT", "limitOrder":{"size": "'+ str(size) + '","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}'
    print(place_order_Req)

df.apply(test, axis=1)

答案 1 :(得分:0)

我现在已经检查了代码,它可以与以下代码一起使用,但我仍然认为必须有一个比使用两个循环更好的方法:

with tf.device('device:XLA_GPU:0'):
  a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')

答案 2 :(得分:0)

您可以使用apply

def query(marketId, selectionId, size):

    global place_order_Req

    place_order_Req = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"' + marketId + '","instructions":'\
                      '[{"selectionId":"' + str(selectionId) + '","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"' \
                      + str(size) + '","price":"1.1","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}'

    return place_order_Req

marketId = "1.156196315"
queries = df.apply(lambda x: query(marketId, x.selectionId, x.size), axis=1)

queries_list = queries.values.tolist() # list of your queries strings