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
  Parse XML String in Pandas Dataframe creedX 2 156 Dec-09-2019, 07:35 PM
Last Post: creedX
  Pandas dataframe to join three tables using like condition among them sandeep_ganga 0 186 Nov-29-2019, 08:30 AM
Last Post: sandeep_ganga
  Pandas Dataframe to Google Big Query Ecniv 2 760 Nov-21-2019, 02:26 PM
Last Post: Ecniv
  manipulating a dataframe - pandas nsx200 2 169 Nov-14-2019, 10:38 AM
Last Post: nsx200
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 144 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  pandas dataframe iloc mystery edvvardbrian 2 215 Oct-29-2019, 02:55 PM
Last Post: jefsummers
  How to add a few empty rows into a pandas dataframe python_newbie09 2 806 Sep-20-2019, 08:52 AM
Last Post: python_newbie09
  Dropping a column from pandas dataframe marco_ita 6 1,056 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  created a pandas series instead of pandas DataFrame ibaad1406 6 679 Sep-06-2019, 06:23 AM
Last Post: ibaad1406
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 407 Sep-04-2019, 10:04 PM
Last Post: Nuovoq

Forum Jump:


Users browsing this thread: 1 Guest(s)