0

I write the below code for my project but dataframe df showing empty records.I want to know where i am lacking in the code:

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])

getting errror as:

UnboundLocalError: local variable 'df' referenced before assignment
Saurabh
  • 21
  • 7

1 Answers1

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

. Here's an MCVE of how your code should look:

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']

New 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)
Latika Agarwal
  • 973
  • 1
  • 6
  • 11
James
  • 274
  • 4
  • 12
  • It give me error as: UnboundLocalError: local variable 'df' referenced before assignment – Saurabh Jun 15 '18 at 06:15
  • Try naming your columns after appending to your df. – James Jun 15 '18 at 06:17
  • @U8-Forward it resolve the problem of UnboundLocalError: local variable 'df' referenced before assignment but the dataframe is still empty – Saurabh Jun 15 '18 at 06:26
  • Please look at the MCVE. – James Jun 15 '18 at 06:27
  • Correct. You just need to add a 'df = pd.DataFrame()' above your 'def' which I forgot to put into my MCVE! – James Jun 15 '18 at 06:54
  • Otherwise should work. Check my updated MCVE to see what I mean. Basically you're creating an empty dataframe and you're adding data to the container. – James Jun 15 '18 at 06:55
  • Which error? Also copy my bottom block of code into your IDE and try it out – James Jun 15 '18 at 06:58
  • your code work correctly but when i performed on my code it giving me error as:local variable 'df' referenced before assignment – Saurabh Jun 15 '18 at 07:03
  • Oh I see. You changed your code and put in a function. Put the df = pd.DataFrame() right below def urlmake(). – James Jun 15 '18 at 07:08
  • it is showing another error: DataFrame constructor not properly called! . – Saurabh Jun 15 '18 at 07:12
  • Just copy my second MCVE and build around it. In the spirit of learning, I would also suggest reading about local and global variables as it may help with your particular issues. – James Jun 15 '18 at 07:18
  • i want to ask if we have multiple records which are to be passed in the function then every time value of value of dataframe df will changed because each time we are creating the dataframe so how can we preserve the value of df – Saurabh Jun 15 '18 at 08:41
  • Because df is defined locally inside the function. The variable df is destroyed every time your compiler reaches the end of the function body. Therefore I suggest not using a function. Again, this goes in line with reading about local and global variables. – James Jun 15 '18 at 09:27
  • Any alternative for this method so that this problem resolved – Saurabh Jun 15 '18 at 09:32