0

I wanted to add a row to my dataframe using a function

features = pd.read_csv("Work.csv")

here is the function :

def classify(Res,O,S,R,Rear,F,L,B):    
     features = pd.DataFrame(np.array([[Res,O,S,R,Rear,F,L,B]]), 
                      columns=['Resolution','OS','Storage','RAM',
                       'RearCamera','FrontCamera','LoudSpeaker','Battery'])\
                  .append(features, ignore_index=True)

when I call the function as :

classify(19283,"lolipop","fr","gg","ff",0,0,0)

I get the following error :

 ---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-4-7c16987ef1b2> in <module>()
----> 1 classify(19283,"lolipop","fr","gg","ff",0,0,0)

<ipython-input-3-c620ec52544a> in classify(Res, O, S, R, Rear, F, L, B)
      1 def classify(Res,O,S,R,Rear,F,L,B):
      2 
----> 3     features = pd.DataFrame(np.array([[Res,O,S,R,Rear,F,L,B]]), columns=['Resolution','OS','Storage','RAM','RearCamera','FrontCamera','LoudSpeaker','Battery']).append(features, ignore_index=True)

UnboundLocalError: local variable 'features' referenced before assignment

How do I fix it ?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Kiran Pun
  • 41
  • 4
  • 1
    How can you append `features` to `features` if `features` has not been defined yet? It is not clear what you want to do, but removing `.append(features, ignore_index=True)` would eliminate the error message. – DYZ Jul 24 '18 at 04:48
  • I want to add a new row in a pre existing data frame using a function , i have defined feature as : features = pd.read_csv("Work.csv") – Kiran Pun Jul 24 '18 at 04:53
  • Please add the missing line to your example. The problem that you encounter depends on the location of that line. – DYZ Jul 24 '18 at 04:55
  • I made made a change . Hope it is more clearer now . – Kiran Pun Jul 24 '18 at 04:58

0 Answers0