Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

python - Pandas: apparent type mismatch using apply

Say I want to apply a function to user_location

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
....

Why am I getting a TypeError (argument of type 'float' is not iterable) doing

def get_country(s):
    for country in pycountry.countries:
        if country.name in s:
            return country.name

d = df["user_location"].apply(get_country)

while get_country('Some text about Canada') works like a charm

---------------------------------------------------------------------------
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

If apply really apply get_country element wise, why is it complaining about float not being iterable? I tried to set convert_dtype=False just in case but it did not change a thing.

question from:https://stackoverflow.com/questions/65838907/pandas-apparent-type-mismatch-using-apply

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe some of user_location got mixed with numbers? Just force them "if country.name in str(s):"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...