说我要给user_locationapply
一个功能
df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 4139 entries, 2020-12-20 06:06:44 to 2021-01-13 04:45:10
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 id 4139 non-null int64
1 user_name 4139 non-null object
2 user_location 3282 non-null object
....
为什么我得到TypeError
(类型为'float'的参数不可迭代)做
def get_country(s):
for country in pycountry.countries:
if country.name in s:
return country.name
d = df["user_location"].apply(get_country)
虽然get_country('Some text about Canada')
像魅力
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-89-50fde3ed5073> in <module>
6 countries = [country.name for country in pycountry.countries]
7
----> 8 d = df["user_location"].apply(get_country)
9
10 df["user_location"]
~/.local/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
4106 else:
4107 values = self.astype(object)._values
-> 4108 mapped = lib.map_infer(values, f, convert=convert_dtype)
4109
4110 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-89-50fde3ed5073> in get_country(s)
1 def get_country(s):
2 for country in pycountry.countries:
----> 3 if country.name in s:
4 return country.name
5
TypeError: argument of type 'float' is not iterable
如果apply
真的在get_country
元素上明智地应用,为什么它抱怨float
不可迭代?我试图设置convert_dtype=False
以防万一,但并没有改变任何事情。
也许某些user_location混入了数字?强迫他们"if country.name in str(s):"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句