Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:12:45+00:00 2026-06-15T03:12:45+00:00

I just picked up pandas , thinking that it will enable me to do

  • 0

I just picked up pandas, thinking that it will enable me to do data analysis nicely in python. Now I have a pandas data frame of the following form:

pandas.DataFrame({"p1": [1, 1, 2, 2, 3, 3]*2,
                  "p2": [1]*6+[2]*6,
                  "run": [1, 2]*6,
                  "result": xrange(12)})

    p1  p2  result  run
0    1   1       0    1
1    1   1       1    2
2    2   1       2    1
3    2   1       3    2
4    3   1       4    1
5    3   1       5    2
6    1   2       6    1
7    1   2       7    2
8    2   2       8    1
9    2   2       9    2
10   3   2      10    1
11   3   2      11    2

I would like to generate the frame that contains one entry for every set of parameters p1 and p2 with the average of all values of result for these parameters, that is,

   p1  p2  result
0   1   1     0.5
1   2   1     2.5
2   3   1     4.5
3   1   2     6.5
4   2   2     8.5
5   3   2    10.5

What is the pandas way to do this? I would try to copy the original table, drop columns that differ (result and run), reindex that, combine both things again with the new index as multi-index and then run the mean method for that outer multi-index level. Is that the way to do it, and if yes, how do I do these index things properly in code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:12:46+00:00Added an answer on June 15, 2026 at 3:12 am

    You can use groupby (I have called your dataframe df):

    df.groupby(['p1', 'p2']).mean()
    

    This results in a MultiIndex DataFrame. To get the layout in your question, select only the columns you want and reset the index:

    df.groupby(['p1', 'p2']).mean()['result'].reset_index()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just picked up Resharper and have been playing around converting fields to properties.
Warning : I have just picked up Maven, so things mentioned might be wrong
I just noticed that a disabled input text field is not picked up in
I just want to have a check to make sure the user picked a
I just picked up an existing web application at work that is supposed to
I just picked up a job where I'm to make some changes to a
I just picked up the Google API today to allow some users of our
I just had an idea, is that possible to protect my java sources (packed
Just checking my JS and I have an error, but I cannot see where.
Just a knowledge question which I would like to ask: For example, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.