简体   繁体   中英

Attribute Error: 'float' object has no attribute 'split' when make word bag from pandas dataframe

here's my dataframe l2_name

    l2_name
0   Camp Site
1   Theme Park
2   Beach
3   others
4   Domestic Airport

Here's what I try

l2 = [set(x.split()) for x in l2_name['l2_name'].str.lower()]

Error message

AttributeError                            Traceback (most recent call last)
<ipython-input-23-5de5cc938292> in <module>
----> 1 l2 = [set(x.split()) for x in l2_name['l2_name'].str.lower()]

<ipython-input-23-5de5cc938292> in <listcomp>(.0)
----> 1 l2 = [set(x.split()) for x in l2_name['l2_name'].str.lower()]

AttributeError: 'float' object has no attribute 'split'

What's wrong, the code works on other dataframe

If there are strings and misisng values you can remove them before converting to lowercase:

l2 = [set(x.split()) for x in l2_name['l2_name'].dropna().str.lower()]

If possible some numbers in data and str.lower convert them to NaN s remove values after:

l2 = [set(x.split()) for x in l2_name['l2_name'].str.lower().dropna()]

Another solution:

l2 = l2_name['l2_name'].str.lower().dropna().str.split().apply(set).tolist()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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