Aggregate, Transpose, and pull in value in Pandas Dataframe

随声附和 提交于 2021-02-09 07:24:06

问题


Input DF:

ID Time Value 
0   1    5
0   2    7
0   3    8
1   1    1
1   2    4
1   3    6

Output DF:
   1  2  3
0  5  7  8
1  1  4  6 

Goal: I currently have something similar to the input DF and am looking to transform it into the output DF.

  • Row 1 of the output DF is equal to the unique time data points.
  • Column 1 of the output DF is equal to the unique IDs. The remaining
  • center points are equal the Value element given the id/time

the closest I've gotten is by doing something like this:

group_by = input_df.groupby('ID').agg({'Value':np.mean})

Or:

group_by = input_df.groupby('time').agg({'Value':np.mean})

This will get me aggregate means rolled up by ID or Time, but I can't figure out how to do both and just pull in the value.


回答1:


You can use pivot:

df.pivot(index='ID', columns='Time', values='Value')
Out: 
Time  1  2  3
ID           
0     5  7  8
1     1  4  6

This assumes that Time/ID pairs are unique. If not, you can replace that with pivot_table and add a suitable aggregate function:

df.pivot_table(index='ID', columns='Time', values='Value', aggfunc='first')
Out: 
Time  1  2  3
ID           
0     5  7  8
1     1  4  6

Your approach would also work with unstack:

df.groupby(['ID', 'Time'])['Value'].agg('mean').unstack()
Out: 
Time  1  2  3
ID           
0     5  7  8
1     1  4  6



回答2:


import pandas as pd

df = pd.DataFrame([(0, 1, 5), (0, 2, 7), (0, 3, 8), (1, 1, 1), (1, 2, 4), (1, 3, 6)], columns=['ID', 'Time', 'Value'])
df

Out[5]: 
   ID  Time  Value
0   0     1      5
1   0     2      7
2   0     3      8
3   1     1      1
4   1     2      4
5   1     3      6

df.pivot(index='ID', columns='Time', values='Value')

Out[7]: 
Time  1  2  3
ID           
0     5  7  8
1     1  4  6


来源:https://stackoverflow.com/questions/40981922/aggregate-transpose-and-pull-in-value-in-pandas-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!