I have a pandas dataframe that look like this :
ID, tag, score1
A1, T1, 10
A1, T1, 0
A1, T2, 20
A1, T2, 0
A2, T1, 10
A2, T1, 10
A2, T2, 20
A2, T2, 20
Using pandas pivot_table function, I am able to pivot the table in order to obtain the following dataframe :
df.pivot_table(index= 'tag' , columns='ID', values= 'score1' , aggfunc='mean')
A1, A2
T1 5, 10
T2 10, 20
Now let's say that my input dataframe have multiple score
columns :
ID, tag, score1, score2, score3
A1, T1, 10, 100, 1000
A1, T1, 0, 0, 0
A1, T2, 20, 200, 2000
A1, T2, 0, 0, 0
A2, T1, 10, 100, 1000
A2, T1, 10, 100, 1000
A2, T2, 20, 200, 2000
A2, T2, 20, 200, 2000
And I am looking for a way to pivot
the data to get the following result :
df.pivot_table(index= ??? , columns='ID', values= ??? , aggfunc='mean').round(-3)
A1, A2
score1 7.5, 15
score2 75, 150
score3 750, 1500
This time I don't want to pivot using values of a column, but directly using multiple column names.
Is there a way to do this using pivot_table()
or am I going in the wrong direction ?
Aggregate mean
and then transpose by T
:
df = df.groupby('tag').mean().T
print (df)
tag T1 T2
score1 7.5 15.0
score2 75.0 150.0
score3 750.0 1500.0
Yes, you can use pivot_table
like this:
df1.pivot_table(columns='ID', aggfunc='mean')
Output:
ID A1 A2
score1 7.5 15.0
score2 75.0 150.0
score3 750.0 1500.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.