【问题标题】:code showing empty dataframe显示空数据框的代码
【发布时间】:2018-06-15 06:08:31
【问题描述】:

我为我的项目编写了以下代码,但数据框 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

【问题讨论】:

  • 运行该代码时会得到什么输出?
  • @Harlekuin 我已经编辑了我的问题
  • 我的意思也是 print(),但 David 回答你的问题了吗?
  • 我之前已经完成了我的代码,但给我的错误是:UnboundLocalError: local variable 'df' referenced before assignment

标签: python python-2.x pysolr


【解决方案1】:
    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)

【讨论】:

  • 它给我的错误是:UnboundLocalError: local variable 'df' referenced before assignment
  • 尝试在附加到您的 df 后命名您的列。
  • @U8-Forward 解决了 UnboundLocalError: local variable 'df' referenced before assignment but the dataframe is empty
  • 请查看 MCVE。
  • 正确。你只需要在你的'def'上方添加一个'df = pd.DataFrame()',我忘了把它放进我的MCVE!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多