Python Forum

Full Version: Trying to search and make new column from the keyword.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Below is the code - I am trying to add new column as name - keywords and data frame name is qd1 and column name is CRTXT
The error I am getting is
Error:
: 'float' object has no attribute 'upper'()


def keyword(row):
  strings = row['CRTXT']
  keywords =  ["stopped working","quit working","didnt like","stop working","not working"]
  keyword = [key for key in keywords if key.upper() in strings.upper()]
  return ','.join(keyword)


df1 = pd.DataFrame(qd1,columns = ['CRTXT'])
df1['keyword'] = df1.apply(keyword, axis=1)
The error says that you have a float number in df1 which means you have a float number in qd1. From the screenshot this must be between rows 4 and 73511. The code below has the same problem, crashing when it tries to call (2.0).upper().
import pandas as pd

df1 = pd.DataFrame(["-", "+", "1", 2.0], columns=["CRTXT"])
for x in df1['CRTXT']:
    print(x, ', ', sep="", end="")
    print(x.upper())
Output:
-, - +, + 1, 1 2.0, Traceback (most recent call last): File "...", line 6, in <module> print(x.upper()) AttributeError: 'float' object has no attribute 'upper'