简体   繁体   English

'float' object 不可迭代",'发生在索引 1'

[英]'float' object is not iterable", 'occurred at index 1'

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.我有一个自定义的 function,它将 Mate、Bence、Raul 和 Marina 列与“company_name_Ignite”列进行比较,并针对 company_name_Ignite 列返回每列的相似度分数。

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?由于我没有在代码中看到任何错误,我能否就为什么会发生这种情况获得一些帮助?

There are missing values, so possible idea is use if-else statemen with pandas.notna :缺少值,所以可能的想法是使用 if-else 语句和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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM