-1

I have a data science project where I scraped information about cars. I am trying to perform stats function to prove if the information is statistically relevant. Other parts of the project I was testing was features in car, which was relatively straight forward as the cars either had the feature or didn't. In other words, 0 or 1. But for the other parts of the project like expert ratings it goes between anything between 3.1 to 4.8, I am trying to split this into two groups so I can perform a T test on it.

Here is my code

KBB_df.Expert-Rating[KBB_df.Expert-Rating <= ('3.6')] = 0
KBB_df.Expert-Rating[KBB_df.Expert-Rating > ('3.6')] = 1
print(Expert-Rating) 

Here is the error that I was given SyntaxError: can't assign to operator What is the problem?

AEzzat
  • 1
  • `Expert-Rating` doesn't make sense as an identifier. It contains an operator hence is not a legal name. Did you mean `Expert_Rating`? This question seems like it is a simple typo. – John Coleman Jul 25 '20 at 00:35
  • It's possible they didn't select the column names in the first place. This might be a helpful question, which is similarly about columns with names that aren't valid Python identifiers: https://stackoverflow.com/questions/13757090/pandas-column-access-w-column-names-containing-spaces – Weeble Jul 25 '20 at 00:49
  • @Weeble Good point. I didn't think of that possibility. – John Coleman Jul 25 '20 at 00:51
  • @Weeble thanks I forgot to check the name of the columns everything is now working, unfortunately the csv came with that column name like that, will have to be more careful in the future – AEzzat Jul 25 '20 at 20:31

1 Answers1

2

You can't use - as part of an identifier in Python. Python thinks you are doing a subtraction, and it's complaining "can't assign to operator" because from its point of view you're doing a - b = 0 where a is KBB_df.Expert and b is Rating[KBB_df.Expert-Rating <= ('3.6')].

If you can't rename your columns, you can do KBB_df['Expert-Rating'] instead of KBB_df.Expert-Rating to access the column when it has characters that aren't allowed in identifiers.

Weeble
  • 17,058
  • 3
  • 60
  • 75