1

enter image description here

This is my dataframe. How to I add max_value, min_value, mean_value, median_value names to rows so that my index values will be like

0

1

2

3

4

max_value

min_value

mean_value

median_value

Could anyone help me in solving this

2
  • i need to calculate for both row and column wise Commented Feb 6, 2019 at 7:22
  • oh okay now got it :) Commented Feb 6, 2019 at 7:23

3 Answers 3

3

If want add rows use add DataFrame.agg:

df1 = df.append(df.agg(['max','min','mean','median']))

If want add columns use assign with min, max, mean and median:

df2 = df.assign(max_value=df.max(axis=1),
                min_value=df.min(axis=1),
                mean_value=df.mean(axis=1),
                median_value=df.median(axis=1))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @jezrael but I'm getting each value twice like two times max,min,mean and median but thanks a lot i got the solution
@Yadhu - So only use df1 = df.agg(['max','min','mean','median']), my solution also add columns and rows
2

one Way is,

Thanks to @jezrael for the help.

df = pd.DataFrame(np.random.randint(0,100,size=(5, 4)), columns=list('ABCD'))
df1=df.copy()

#column wise calc
df.loc['max']=df1.max()
df.loc['min']=df1.min()
df.loc['mean']=df1.mean()
df.loc['median']=df1.median()

#row wise calc
df['max']=df1.max(axis=1)
df['min']=df1.min(axis=1)
df['mean']=df1.mean(axis=1)
df['median']=df1.median(axis=1)

O/P:

           A     B     C     D   max   min   mean  median
0       49.0  91.0  16.0  17.0  91.0  16.0  43.25    33.0
1       20.0  42.0  86.0  60.0  86.0  20.0  52.00    51.0
2       32.0  25.0  94.0  13.0  94.0  13.0  41.00    28.5
3       40.0   1.0  66.0  31.0  66.0   1.0  34.50    35.5
4       18.0  30.0  67.0  31.0  67.0  18.0  36.50    30.5
max     49.0  91.0  94.0  60.0   NaN   NaN    NaN     NaN
min     18.0   1.0  16.0  13.0   NaN   NaN    NaN     NaN
mean    31.8  37.8  65.8  30.4   NaN   NaN    NaN     NaN
median  32.0  30.0  67.0  31.0   NaN   NaN    NaN     NaN

4 Comments

@jezrael - please take a look now
I think new columns, OP need new columns also
@jezrael - I'm confused here, please take a serious look at OP's input image. that already contains computed value.
check comment i need to calculate for both row and column wise
1

This worked well and fine:

df1 = df.copy()
df.loc['max']=df1.max()
df.loc['min']=df1.min()
df.loc['mean']=df1.mean()
df.loc['median']=df1.median()

6 Comments

It has a problem, Don't take this code. while calculating mean median it consider values from max and min columns, so you will end up with wrong calculated values. But If you know the columns you want to perform arithmetic operations then instead of df pass df[req_cols] then perform your arithmetic ops.
Yes, exactly. Test it in small data sample - it is wrong.
@jezrael - Is there any way to assign multiple columns in a single line?
Check my answer, agg function
@MohamedThasinah - Edited this answer.
|

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.