0

我为我的项目编写了以下代码,但数据框 df 显示空记录。我想知道代码中缺少的地方:

import urllib
from urllib2 import *
import pandas as pd
def urlmake(req):
    requests = [req]
    for parms in requests:
        url = 'http://localhost:8983/solr/data/select?indent=on&' + urllib.urlencode(parms)
        connection = urlopen(url)
        response = eval(connection.read())
        t = response['response']['numFound']
        req2 = req['q'][13:17]
        print(req2)
        if(req2 == 'AXIS'):
            print('true')
            for i in range(0,t):
                t1 = float((response['response']['docs'][i]['message']).split(" ")[1])
                #print(t1)
                t2 = response['response']['docs'][i]['customer_id']
                #print(t2)
                df = df.append(pd.DataFrame(t2,t1))




ba_query = [{'q':'sender_name:*AXIS*  AND message:*Avbl Lmt*','start':0,'rows':211,'wt':'json'}]


for i in range(0,len(ba_query)):
    urlmake(ba_query[i])

错误如下:

UnboundLocalError: local variable 'df' referenced before assignment
4

1 回答 1

0
    import urllib
    from urllib2 import *
    import pandas as pd
    df = pd.DataFrame(columns=['Customer_id','Spent'])
    def urlmake(req):
    requests = [req]
    for parms in requests:
        url = 'http://localhost:8983/solr/data/select?indent=on&' + urllib.urlencode(parms)
        connection = urlopen(url)
        response = eval(connection.read())
        t = response['response']['numFound']
        req2 = req['q'][13:17]
        print(req2)
        if(req2 == 'AXIS'):
            print('true')
            for i in range(0,t):
                t1 = float((response['response']['docs'][i]['message']).split(" ")[1])
                #print(t1)
                t2 = response['response']['docs'][i]['customer_id']
                #print(t2)
                df = df.append({'Customer_id':t2, 'Spent':t1}, ignore_index=True)  # HERE

See the comment in the code

. 这是您的代码外观的 MCVE:

import pandas as pd
import numpy as np

df = pd.DataFrame()

for iteration in range(0, 5):
    dummy_data = np.random.rand(3, 3)
    df = df.append(pd.DataFrame(dummy_data))

df.columns = ['a', 'b', 'c']

新的 MCVE:

import pandas as pd
import numpy as np

def myfunc():
    df = pd.DataFrame()
    for iteration in range(0, 5):
        dummy_data = np.random.rand(3, 3)
        df = df.append(pd.DataFrame(dummy_data))
    df.columns = ['a', 'b', 'c']
    return df

df2 = myfunc()
print(df2)
于 2018-06-15T06:13:14.530 回答