Ymi
Ymi

Reputation: 768

Pandas: Extracting data from sorted dataframe

Consider I have a dataframe with 2 columns: the first column is 'Name' in the form of a string and the second is 'score' in type int. There are many duplicate Names and they are sorted such that the all 'Name1's will be in consecutive rows, followed by 'Name2', and so on. Each row may contain a different score.The number of duplicate names may also be different for each unique string.'

I wish to extract data afrom this dataframe and put it in a new dataframe such that There are no duplicate names in the name column, and each name's corresponding score is the average of his scores in the original dataframe.

I've provided a picture for a better visualization: x need not necessarily be equal to y.

Upvotes: 0

Views: 70

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24324

Firstly make use of groupby() method as mentioned by @QuangHong:

result=df.groupby('Name', as_index=False)['Score'].mean()

Finally make use of rename() method:

result=result.rename(columns={'Score':'Avg Score'})

Upvotes: 1

Related Questions