0

What is the difference between

df['Good Quality'] = [1 if x>=7 else 0 for x in df['quality']]

and

for x in df['quality']:
    if x>=7:
        df['Good Quality'] = 1
    else:
        df['Good Quality'] = 0

data frame is not changing for x>=7 if I used the second one? What is the logical error happening here?

  • 2
    Both are semantically wrong and you shouldn't iterate over a pandas object (good [read](https://stackoverflow.com/a/55557758/9081267) here). When you use pandas / numpy, we do operations on columns / vectors. I would suggest to read the basics in the [documentation](https://pandas.pydata.org/pandas-docs/stable/user_guide/10min.html). If you want to iterate over objects, then just use dicts / lists. – Erfan Jul 02 '21 at 09:45

1 Answers1

0

You need to mention that for loop in the pythonic style syntax

for x in df['quality']:
    if x>=7:
        df['Good Quality'] = 1
    else:
        df['Good Quality'] = 0
Edit : I forgot about list result
result = []
for x in df['quality']:
    if x>=7:
         result.append(1)
    else:
         result.append(0)

df['Good Quality'] = result

You may test this.

Rizquuula
  • 578
  • 5
  • 15