0

I have two columns and I need to check whether the value in one column, all_news['Query], is in another column, all['description'] column.

I found the following solution:

all_news['C'] = on.apply(lambda x: x.Query in x.description, axis=1)

but I get the following error:

TypeError: ("argument of type 'float' is not iterable", 'occurred at index 737')

Likely because there are some weird characters the iteration cannot decipher and it seems I cannot run any exception management in a one-line iteration.

How can I unfold this one line iteration into a for loop?

Result for index 737:

Query = 'medike international'

description = 'po ketvirtadienį praūžusios liūties nukentėjo ne tik kauno miestas, bet ir rajonas. pliaupiant lietui prie vilkijos vydūno alėjos esančioje apžvalgos aikštelėje ...'

Filippo Sebastio
  • 1,112
  • 1
  • 12
  • 23
  • Have you checked the answers on [this](https://stackoverflow.com/questions/19960077/how-to-filter-pandas-dataframe-using-in-and-not-in-like-in-sql) question? – Erfan Nov 20 '19 at 10:42
  • Have you maybe tried to set the ```description```column of your DataFrame to string type? Something like df["description"] = df["description"].astype(str)``` ? It looks like pandas is reading a float at a certain index. – bglbrt Nov 20 '19 at 10:43
  • @tibbles I tried, but no success. – Filippo Sebastio Nov 20 '19 at 10:45
  • @erfan is not a perfect match, df['Query'] is often a substring of df['description']..therefore they would not merge. – Filippo Sebastio Nov 20 '19 at 10:45
  • Include a small example dataset. You probably need `str.contains`, you don't need a for loop – Erfan Nov 20 '19 at 10:46
  • And could you by any chance display what the columns ```Query``` and ```description``` give at index 737? – bglbrt Nov 20 '19 at 10:46
  • I added the results for all_news.iloc[737:738,4:6] – Filippo Sebastio Nov 20 '19 at 10:54
  • The strings both look fine... but just to make sure: do you want to check if the string in ```query``` is contained in the string in ```description``` for the same row, or if the string in ```query``` is contained in the overall ```description``` column? – bglbrt Nov 20 '19 at 10:57
  • In the latter case, why wouldn't something simpler like ```all_news['C'] = all_news["Query"].isin(all_news["description"])``` work? – bglbrt Nov 20 '19 at 10:58
  • I guess it can work also in the latter case, more expensive in terms of approach, but it does the trick – Filippo Sebastio Nov 20 '19 at 11:00

0 Answers0