python 遍历字典列表并将它们存储到单独的列中

bprjcwpo  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(145)

https://github.com/ajayvd/DOUBT_SO/blob/main/doubt.csv
上面的链接包含 Dataframe ,它包含2列response_value & response_errors & response_errors包含字典列表中的值。

问题陈述

所以这里我只想迭代提供者键,并将它们存储在列表形式的列中
例如:
对于第1行,它将是[ shipt ]
第2行[装运]
第3排[ doordash,pickupnow,roadie ]

你需要做预处理,所以我在下面附加了一段代码。

def parse_json(webhook_data):
    try:
        data = json.loads(webhook_data)
    except ValueError as ex:
        data={'estimate':'error'}
    return data

data_metering['RESPONSE_ERRORS']=data_metering['RESPONSE_ERRORS'].apply(parse_json)

自定义函数我的代码,请修改代码,并告诉我优化的方式来做它。

def traverse_dsp(data_frame,column):
    provider = []
    

    #Iterate over each row in Dataframe
    for index, row in data_frame.iterrows():
       # Iterate over each json object in each row in DataFrame
        for i in range(0,len(row[column])):

            for k,v in row[column][i].items():

                if k=="provider":

                    provider_val=v
                    
                    x.loc[index,"provider_list"]=provider
                    
           
x=data_metering
traverse_dsp(x,"RESPONSE_ERRORS")
gfttwv5a

gfttwv5a1#

IIUC,你可以试试这个:

from ast import literal_eval
​
df_ = pd.read_csv("https://raw.githubusercontent.com/ajayvd/DOUBT_SO/main/doubt.csv")
​
ser = df_["RESPONSE_ERRORS"].apply(literal_eval).explode()
​
df = (df_.join(pd.DataFrame(ser.tolist(), index=ser.index)
                       .groupby(level=0)["provider"].agg(list)))

输出:

print(df["provider"])

0                          [Shipt]
1                          [Shipt]
2    [DoorDash, PickupNow, Roadie]
3                       [DoorDash]
4                       [DoorDash]
Name: provider, dtype: object

print(df)

                     RESPONSE_VALUE                                                                                                                                                                                                                                                                                                                                                                                                                                        RESPONSE_ERRORS provider_list                       provider
0                         ['Shipt']                                                                                                                                                                                                                                                                                                                               [{'code': 0, 'message': "Shipt doesn't provide estimates", 'provider': 'Shipt', 'type': 'DSP_DOES_NOT_OFFER_ESTIMATES'}]         shipt                        [Shipt]
1                         ['Shipt']                                                                                                                                                                                                                                                                                                                               [{'code': 0, 'message': "Shipt doesn't provide estimates", 'provider': 'Shipt', 'type': 'DSP_DOES_NOT_OFFER_ESTIMATES'}]           NaN                        [Shipt]
2                         ['FedEx']  [{'code': 400, 'message': 'Distance from pickup address to dropoff address exceeds 15 miles (24 kilometers): dropoff_address', 'provider': 'DoorDash', 'type': 'MAX_DISTANCE_EXCEEDED'}, {'code': 0, 'message': 'Not serviceable, Reason: Mileage', 'provider': 'PickupNow', 'type': 'UNAVAILABLE'}, {'code': 400, 'message': 'Route Sorry, distance in passenger vehicles is limited to 30 miles.', 'provider': 'Roadie', 'type': 'UNMAPPED_ERROR'}]           NaN  [DoorDash, PickupNow, Roadie]
3  ['PickupNow', 'FedEx', 'Roadie']                                                                                                                                                                                                                                                               [{'code': 400, 'message': 'Distance from pickup address to dropoff address exceeds 15 miles (24 kilometers): dropoff_address', 'provider': 'DoorDash', 'type': 'MAX_DISTANCE_EXCEEDED'}]           NaN                     [DoorDash]
4                        ['Roadie']                                                                                                                                                                                                                                                               [{'code': 400, 'message': 'Distance from pickup address to dropoff address exceeds 15 miles (24 kilometers): dropoff_address', 'provider': 'DoorDash', 'type': 'MAX_DISTANCE_EXCEEDED'}]           NaN                     [DoorDash]

相关问题