0

I have a dataframe with different info about arrests.

arrests_df["is_violent"] = [
    1 if x == "Battery" or x == "Assault" else 0 for x in arrests_df["statute_desc"]
]

So in the column called "statute_desc", every time the word battery or assault is mentioned, put 1. But this code doesn't work.

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
  • 3
    What about the code doesn't work? – Lord Elrond May 11 '20 at 18:14
  • It just puts "0" every time in the new column, even when the original row contains the word "Battery" or "Assault". The row contains a bunch of words. So it'll be like "Third-Degree Assault". That might help – Sample Name May 11 '20 at 18:15
  • Based on the above you want `"Battery" in x or "Assault" in x`. `==` is not a containment operator – Cireo May 11 '20 at 18:17
  • Please provide sample data, your expected outcome, and the actual outcome. As is, this is not a *[minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)* – Lord Elrond May 11 '20 at 18:19

1 Answers1

0

Per my comment:

You want "Battery" in x or "Assault" in x. == is not a containment operator.

'a' == 'abc'  # False
'a' in 'abc'  # True

Likewise:

"Third-Degree Assault" == 'Assault'   # False
'Assault' in "Third-Degree Assault"  # True

and thus:

>>> data = ["Third-Degree Assault", "Assault", "Foobar"]
>>> [1 if x == "Battery" or x == "Assault" else 0 for x in data]
[0, 1, 0]
>>> [1 if "Battery" in x or "Assault" in x else 0 for x in data]
[1, 1, 0]
Cireo
  • 4,197
  • 1
  • 19
  • 24
  • can you write the whole line of code? Cause I get this error: TypeError: argument of type 'float' is not iterable – Sample Name May 11 '20 at 18:20
  • You should check your data then, is it possible that `"statute_desc"` contains floats instead of strings? – Cireo May 11 '20 at 18:23
  • i get TypeError: argument of type 'float' is not iterable – Sample Name May 11 '20 at 18:24
  • That's what you said the first time. Try using `collections.Counter(map(type, arrests_df["statute_desc"]))`. That being said, it seems like this question was closed (as a duplicate of a mostly unrelated question). – Cireo May 11 '20 at 18:25
  • Yeah I don't know why they closed it. And I get "name 'collections' is not defined" now. Thank you for your help though, even though the question is closed. – Sample Name May 11 '20 at 18:27
  • I have blanks in the column, does that affect anything? – Sample Name May 11 '20 at 18:30
  • you have to run `import collections` – Cireo May 11 '20 at 21:21