0

I have a dataframe and it looks something like this:

[(48500, 53500)]
[(47500, 52500)]
[(45500, 50500)]
[(40700, 45700)]
[(37500, 42500)]
[(37500, 42500)]
[(35000, 40000)]
[(32500, 37500)]
[(32500, 37500)]
[(32500, 37500)]
[(32500, 37500)]
[(32500, 37500)]
[(32500, 37500)]
[(31500, 36500)]
[(31500, 36500)]
[(30419, 35419)]
[(27500, 32500)]
[(27500, 32500)]
[(27500, 32500)]
[(27000, 32000)]
[(26500, 31500)]
[(25000, 30000)]
[(24000, 29000)]
[(23500, 28500)]
[(23420, 28420)]
[(23250, 28250)]
[(20000, 25000)]
[(17500, 22500)]
[(17000, 22000)]

What is the best way to add two numbers together and divide by two?

What I came up with:

for parent in newDf:
    for child in parent:
        print(int(child[0]) + int(child[1]) / 2.0)

I am certain there is a lambda function or one liner that from pandas which could make this task more simple.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Andrew
  • 176
  • 1
  • 14
  • 2
    [`df.mean(axis=1)`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.mean.html) ? – Vladimir Fokow Aug 26 '22 at 14:59
  • Does this answer your question? [Calculate new column as the mean of other columns pandas](https://stackoverflow.com/questions/48366506/calculate-new-column-as-the-mean-of-other-columns-pandas) – mkrieger1 Aug 27 '22 at 08:32
  • Thanks VladimirFokow && mkrieger1. I ideally wanted it it updated within the data frame and not make another frame. – Andrew Aug 27 '22 at 11:20

1 Answers1

1

Sorry I should off posted the whole data frame.

data = {
    "Salary": [(48500, 53500), (47500, 52500),(17000, 22000)],
}
df["Salary"] = df["Salary"].apply(lambda x: np.mean(x))

Output :

    Salary
0  51000.0
1  50000.0
2  19500.0
Andrew
  • 176
  • 1
  • 14
  • 1
    `Salary` is two columns in one. If you split it into two columns (e.g., `df.Salary.apply(pd.Series)`), you can directly do `.mean(axis=1)` as suggested in the comments. By the way, `lambda x: func(x)` is the same as `func`, so you can simply do `.apply(np.mean)` if you don't want to restructure your data. – fsimonjetz Aug 27 '22 at 11:26