遍历字典并将值添加到pandas数据框

时间:2019-01-18 13:49:06

标签: python pandas dataframe

我刚刚了解了熊猫数据框。我有一个嵌套的字典,该字典具有在for循环中提取的值。将这些数据转换为数据帧的最佳方法是什么?


   if result != None:
    for ads in result['data']['ad_list']:

        #print getOnlineProvider(currency)
        if(ads['data']['online_provider'] in onlineprovider):

            #"NATIONAL_BANK") or 
            #(ads['data']['online_provider'] =="SPECIFIC_BANK") or 
            #(ads['data']['online_provider'] =="ALTCOIN_DASH") or 
            #(ads['data']['online_provider'] =="CASH_DEPOSIT")):

            trader = ads['data']['profile']['name']
            adusdprice = ads['data']['temp_price_usd']
            delta =  float(adusdprice) - float(btcprice)
            deltapercent =  (1 -  (float(btcprice)/float(adusdprice))) * 100
            adid = ads['data']['ad_id']
            maxamount = ads['data']['max_amount']
            minamount = ads['data']['min_amount']
            paywindow = ads['data']['payment_window_minutes']

            print("Trader:" + trader + "||" + "Ad ID:" + str(adid) + "||"+ "paymethod: " + ads['data']['online_provider'] + "||" + "USD price: " + adusdprice + "||"+ "min-max: " + str(minamount) + "-" + str(maxamount) +"||" + "PayWindow: " + str(paywindow) + "||" + "Price delta: " + str(delta) + "||"+ "Pct Delta: " + str(deltapercent) + "%") 

print("=====================================================") 

我尝试使用

从字典创建数据框
pd.DataFrame.from_dict(result['data']['ad_list']['data'])

但是据我所知,这是行不通的,因为我需要逐行进行处理,因为我提取了每个值才能正确获取它。

这是我通过上述命令获得的数据帧的结果。

{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 90, u'hidden_by_op...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...
{u'public_view': u'https://localbitcoins.com/a...  {u'require_feedback_score': 0, u'hidden_by_ope...

即时通讯希望以pandas数据框格式获取某些内容,类似于即时通讯在for循环中输出的内容...

交易者:mr_ahmed_btc(1000+; 99%)||广告ID:736624 ||付款方式:CASH_DEPOSIT || USD价格:3866.49 ||最小-最大:200-9185 ||付款窗口:270 ||价格差:260.05 ||三角洲:6.72573833115%

1 个答案:

答案 0 :(得分:0)

from_dict(无假名)期望格言采用以下形式:

{"colname1": [1,2,3,4], "colname2": [2,3,4,5]}

从您的问题中分辨出result['data']['ad_list']['data']的结构并不容易,但我最大的猜测是它类似于:

[
    {
      "dog": "cat",
      "cat": "dog",
      "bug": 234
    },
    {
        "dog": "hot",
        "cat": "tin roof",
        "bug": 432
    }
]

在这种情况下,请使用:

df = pd.DataFrame(result['data']['ad_list']['data'])

这直接从字典构造数据框。

编辑:关于美观打印的问题,请查看Pretty Printing a pandas dataframe

简单打印数据框只需print(df)

但是我不知道这是否可以解决您的问题。听起来result['data']['ad_list']['data']可能是JSON字符串列表,在这种情况下,您需要执行以下操作:

导入json data = json.loads(result ['data'] ['ad_list'] ['data']) df = pd.DataFrame(data)