0
def categorizeMainUrl(url):
    category = "other"
    if "/special/" in url:
        category = "special"
    return category
df["category"] = df["main_URL"].apply(lambda url: categorizeMainUrl(url))

While running this part of the code, I keep the following exception.
"TypeError: argument of type 'float' is not iterable"
How can I select only the section of the dataframe with the float values?
(In this column, I would wait only string as a datatype)

François B.
  • 1,096
  • 7
  • 19

2 Answers2

1

Use Series.fillna to fill NaN values, then you can use Series.str.contains with np.where or Series.map to create a new serie:

df["category"] = np.where(df['main_URL'].fillna('').str.contains('/special/'),
                          "special", "other")

or

df["category"] = (df['main_URL'].fillna('')
                                .str.contains('/special/')
                                .map({True:"special",
                                      False:"other"}) 
                 )
#df['main_URL'].fillna('').str.contains('/special/').replace({True:"special",
#                                                              False:"other"})

I recommend you see: when should I want to use apply

ansev
  • 30,322
  • 5
  • 17
  • 31
0

The following command select only the rows containing on specific datatype (here float):
df[df["category"].apply(lambda x: isinstance(x, float))]

François B.
  • 1,096
  • 7
  • 19
  • 1
    this solutoon works but surely inefficient,I recommend you see: https://stackoverflow.com/questions/54432583/when-should-i-ever-want-to-use-pandas-apply-in-my-code – ansev Jan 14 '20 at 10:32