-1

The general ways of Extracting a column value based on another column in Pandas were not working in my case. I am having a data frame with two variables, Salary and EducationLevels. Salary is having continuous values while EducationLevels has four categories namely 'A', 'B', 'C', and 'D'. I need to get Salary values for EducxationLevel 'A'. The below code is not working:

df['Salary'][df['EducationLevels']=="A]

2 Answers2

0
df[df["EducationLevels"] == "A"]
scapula13
  • 50
  • 1
  • 5
0

df['Salary'][df['EducationLevels']=="A"]

Change this into

df[df['EducationLevels']=="A"]['Salary'].

Another way to filter data is using df.query. For instance, df.query("EducationLevels == 'A'").Salary will do as well.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html

Jose Mar
  • 634
  • 6
  • 8
  • Hi, Thanks for your suggestions. But i got the answer. I included a SPACE before 'A' and it worked. So the final code that worked for me is: df['Salary'][df['EducationLevels']==" A"] – Mystical_soul Aug 23 '22 at 12:06