Python Forum
Referring to a specific element in Pandas Dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Referring to a specific element in Pandas Dataframe
#1
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!
Reply
#2
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...
Reply
#3
Thank you for advising. This was very helpful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 961 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,088 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,266 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,240 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,757 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,525 Jan-10-2022, 04:42 PM
Last Post: moduki1
  Remove specific values from dataframe jonah88888 0 1,686 Sep-24-2021, 05:09 AM
Last Post: jonah88888
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,094 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,417 Jun-22-2021, 07:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020