Home > Blockchain >  TypeError: can only concatenate str (not "numpy.float64") to str data set question
TypeError: can only concatenate str (not "numpy.float64") to str data set question

Time:01-28

Please help Ive spent three hours on stack now i tried ''.join, str, removing the " " for "," and nothing works to remove this error! The last 2 comments is where the error happens! #Description: Build a anime recommendation using python #Store the data df = pd.read_csv('animes.csv') #Show the first 3 rows of data df.head(2)

    #Count of the number of rows/animes in the data set and the number oof colums
    df.shape
    
    
    #List of wanted columns for anime recommendations
    columns =['title','synopsis','genre','aired','episodes']
    
    #Data updated
    df[columns].head(3)
    
    #Missing values check
    df[columns].isnull().values.any()
    
    #Create a funtion to combine the values of the new columns into a single string
    def get_new_features(data):
      new_features =[]
      for i in range (0, data.shape[0]):
         new_features.append(data['title'][i] '' data['synopsis'][i] '' data['genre'][i] '' data['aired'][i] '' data['episodes'][i])
    
         return new_features
     
    
    
     #Create a column to hold combine strings
     df ['new_features'] = get_new_features(df)
    
     #show data
     df.head(4)

    --------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-97-d5676456ab85> in <module>()
          1 #Create a column to hold combine strings
    ----> 2 df ['new_features'] = get_new_features(df)
          3 
          4 #show data
          5 df.head(4)
    
    <ipython-input-95-842623950c0e> in get_new_features(data)
          3   new_features =[]
          4   for i in range (0, data.shape[0]):
    ----> 5      new_features.append(data['title'][i] '' data['synopsis'][i] '' data['genre'][i] '' data['aired'][i] '' data['episodes'][i])
          6 
          7      return new_features
    
    TypeError: can only concatenate str (not "numpy.float64") to str

CodePudding user response:

First off I would print data to see what items/types it contain.

for key, value in data.items():
    print(value)
    print(key)
    print(type(data[key]))

Using this output you should be able to discern what areas in data you need to convert to string or change the formatting of. Most likely you can do the lazy route of just casting each item to string:

new_features.append(str(data['title'][i]) '' str(data['synopsis'][i]) '' str(data['genre'][i]) '' str(data['aired'][i]) '' str(data['episodes'][i]))
  •  Tags:  
  • Related