1

Hello I have the following dataframes:

  df = 
    x val 
    1 5 
    2 10
    5 3

and

  df1 = 
    x val 
    1 2 
    3 4
    4 8
    5 2

I would like to a dataframe that returns the average in the same x.

  df2 = 
    x val 
    1 3.5
    2 10
    3 4
    4 8
    5 2.5

2 Answers 2

2

One way is to Concatenate the dataframes then groupby 'x'.

df2 = pd.concat([df,df1])
df2.groupby('x').mean()

Produces:

    val
x      
1   3.5
2  10.0
3   4.0
4   8.0
5   2.5
Sign up to request clarification or add additional context in comments.

Comments

0

It works with two dataframes. What about if I have different realizations and I want to do other statistical analysis over the different session? Let say I have the following data frame

df
   x val  session
    1 5     0
    2 10    0
    5 3     0
    1 5     1
    2 10    1
    3 4     1
    4 8     1
    5 7     1
    2 6     2
    3 4     2
    5 9     2

What I am doing is something like this:

s_un = pd.unique(df.session)
df_out = pd.Dataframe()
for in s_un:
     df_out = pd.concat([df_out,df[df.session=i]],ignore_index=True)

Is that correct?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.