Python Forum

Full Version: Referring to a specific element in Pandas Dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I need to refer to specific elements in Pandas Dataframe - average salaries in the column "Average". I do not need to refer to the column, I need to refer to these items separately (e.g. only 5166). As I do not know how to do it properly, I created a df with 2 rows and used min and max but it would not work if I had more rows. Is there a solution if there are more rows?

Industry 2018 Q1 ... 2018 Q4 Average
1 Total – all industries 5049 ... 5247 5166
2 Production 4823 ... 5010 4978

def comparison(industry, salary):
    if industry == "production":
        if salary > int(df['Average'].min()):
            return "Above the average salary"
        elif salary < int(df['Average'].min()):
            return "Below the average salary"
        elif salary == int(df['Average'].min()):
            return "Average salary"
    else:
        if salary > int(df['Average'].max()):
            return "Above the average salary"
        elif salary < int(df['Average'].max()):
            return "Below the average salary"
        elif salary == int(df['Average'].max()):
            return "Average salary" 
Hopefully you can advise me.
Thank you!
You can use .iloc to get specific elements, e.g. in last column of the data frame.

df.iloc[1, -1] will return element in the last column that belongs to the second row (0-based indexing in pandas).

If you need to calculate average salaries per industry, you need to look at grouping facilities of pandas, e.g. df.groupby(['industry'])['salary'].mean(). This assumes the df has salary and industry columns.

Hope that helps...
Thank you for advising. This was very helpful.