以匹配的值插入多行

时间:2018-08-08 00:44:35

标签: python pandas

使用熊猫,我想为给定的匹配ID matchid插入多行。

意思是,我目前将结果列表串在一起并插入到现有DF的给定行中:

matchid  |  events_categories 
-----------------------------
0           event_a, event_b, event_c
1           event_b

我想要的是,对于给定的匹配matchid行,在两者之间插入多行:

matchid  |  events_categories 
-----------------------------
0           event_a
0           event_b
0           event_c
1           event_b

我使用的是matchid,因为返回的结果来自异步请求。不能保证顺序中的值。因此,我将响应对象映射回数据框行。

Python:

rs = (grequests.get(u, headers=headers, hooks={'response': 
add_filename_to_response}) for u in urls)
results = grequests.map(rs, exception_handler=exception_handler)

for event_obj in results:
    jsonObj = event_obj.json()
    categories = []
    try:
        for res in jsonObj['results']:
            categories.append(res['category'])
            ### How can I insert at the given row here?
            ### "df.loc[df['matchid']==event_obj.matchid, 'event_categories'] = res['category']" overwrites each value

    except: None

    ### Pair the row to the event_obj via mapping_key

    df.loc[df['matchid']==event_obj.matchid, 'event_categories'] = ', '.join(str(x) for x in categories)

...


'''
In the AJAX response callback, add additional meta data to the response object
'''
def add_filename_to_response(response, *args, **kwargs):
    ### Get filepath from the ?filepath= param in the URL string
    obj_matchid = response.url.split('matchid=')[1].split('&')[0]
    ### Append mapping key to AJAX response object
    response.matchid = obj_matchid

    return response

1 个答案:

答案 0 :(得分:1)

这不是巢

s=df['events_categories'].str.split(',')
pd.DataFrame({'matchid':df['matchid'].repeat(s.str.len()),'events_categories':np.concatenate(s.values)})
Out[517]: 
  events_categories  matchid
0           event_a        0
0           event_b        0
0           event_c        0
1           event_b        1