0

fruits.xlsx

I'm trying to apply a function that:

  1. takes the value of each cell in a column divided by the mean of its respective column.
  2. Then create a column called ['Score'] that has the sum of each cell value in a row computed from step 1.

My code so far:

import pandas as pd
df = pd.DataFrame(pd.read_excel('fruits.xlsx'))
def func(column):
    out = df[column].values / df[column].mean()
    return out

Im really unsure of how to execute this with pandas properly.

1
  • Could you please copy+paste your data as text? Commented Oct 2, 2022 at 22:54

3 Answers 3

1

Try this one will calculate exactly what you need in one single line:

df['Score'] = df.apply(lambda x: sum([x[i]/df[i].mean() for i in df.columns]),axis=1)
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like this

df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]], columns=['a', 'b', 'c'])
df['score'] = df.div(df.mean()).sum(axis=1)

Output

    a   b   c   score
0   1   2   3   1.15
1   4   5   6   3.00
2   7   8   9   4.85

Comments

0

you can make the output as a column in the dataframe

df["Score"] = df[<col_name>] / df[<col_name>].mean()

and you can use

df["Score"] = df[<col_name>].values / df[<col_name>].mean()

I tested both and both gave me the same output in the dataframe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.