Home > Back-end >  'float' object is not iterable", 'occurred at index 1'
'float' object is not iterable", 'occurred at index 1'

Time:01-16

i have for the following dataset

company_name_Ignite      Mate     Bence                     Raul                      Marina 

01 TELECOM LTD           NaN      01 Telecom, Ltd.          01 Telecom, Ltd.          NaN
0404 Investments Ltd     NaN      0404 INVESTMENTS LIMITED  0404 INVESTMENTS LIMITED  NaN

I have got a custom function that compares the Mate, Bence, Raul and Marina columns against the 'company_name_Ignite' column and returns a similarity score for each columns against the company_name_Ignite column.

for col in ['Mate', 'Bence', 'Raul','Marina']:
df[f"{col}_score"] = df.apply(lambda x: similar(x["company_name_Ignite"], x[col]) * 100, 
axis=1)

The problem that I have is that when I try to run the code get the below error:

TypeError                                 Traceback (most recent call last)
<ipython-input-93-dc1c54d95f98> in <module>()
  1 for col in ['Mate', 'Bence', 'Raul','Marina']:
----> 2     df[f"{col}_score"] = df.apply(lambda x: similar(x["company_name_Ignite"], x[col]) 
* 100, axis=1)

c:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, 
broadcast, raw, reduce, result_type, args, **kwds)
6002                          args=args,
6003                          kwds=kwds)
-> 6004         return op.get_result()
6005 
6006     def applymap(self, func):

c:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
140             return self.apply_raw()
141 
--> 142         return self.apply_standard()
143 
144     def apply_empty_result(self):

c:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
246 
247         # compute the result using the series generator
--> 248         self.apply_series_generator()
249 
...
--> 311         for i, elt in enumerate(b):
312             indices = b2j.setdefault(elt, [])
313             indices.append(i)

TypeError: ("'float' object is not iterable", 'occurred at index 1')

Can I please get some help on why this is happening as I don't see any errors in the code?

CodePudding user response:

There are missing values, so possible idea is use if-else statemen with pandas.notna:

for col in ['Mate', 'Bence', 'Raul','Marina']:
    df[f"{col}_score"] = df.apply(lambda x: similar(x["company_name_Ignite"], x[col]) * 100 if pd.notna(x[col]) else np.nan, axis=1)
  • Related