Bottom Page

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!
Quote
#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...
Quote
#3
Thank you for advising. This was very helpful.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas Dataframe to Google Big Query Ecniv 1 82 Aug-21-2019, 04:56 PM
Last Post: ThomasL
  how to apply user defined function to Pandas DataFrame evelynow 3 203 Aug-20-2019, 11:35 PM
Last Post: scidam
  Create dataframe through Dictionary in pandas ift38375 2 123 Aug-11-2019, 01:09 AM
Last Post: boring_accountant
  [pandas] How to re-arrange DataFrame columns SriMekala 8 692 Jun-22-2019, 12:55 AM
Last Post: scidam
  Pandas - updating the original dataframe with sub selects Ecniv 0 155 Jun-21-2019, 02:12 PM
Last Post: Ecniv
  [pandas] Find the first element that is -1 dervast 3 638 Jun-14-2019, 04:58 PM
Last Post: ThomasL
  Simple String to Time within a pandas dataframe Ecniv 1 496 Jun-14-2019, 03:25 AM
Last Post: scidam
  Is there any way to properly load fixed width file into a dataframe using Pandas? vicky53 1 284 Mar-29-2019, 06:04 PM
Last Post: Larz60+
  Manipulating Series in Dataframe (Pandas) wendysling 2 410 Mar-26-2019, 07:12 PM
Last Post: wendysling
  Inserting data from python list into a pandas dataframe mahmoud899 0 348 Mar-02-2019, 04:07 AM
Last Post: mahmoud899

Forum Jump:


Users browsing this thread: 1 Guest(s)