[英]Python & Pandas: match and transpose
The original table is like: 原始表如下:
ID Name Sex Weight Date
1 Bill M 68.1 2015-01-01
2 Mary F 45.2 2015-01-01
3 Gary M 89.2 2015-01-01
1 Bill M 68.3 2015-01-02
2 Mary F 45.3 2015-01-02
3 Gary M 89.1 2015-01-02
The desired output is: 所需的输出是:
ID Name Sex 2015-01-01 2015-01-02
1 Bill M 68.1 68.3
2 Mary F 45.2 45.3
3 Gary M 89.2 89.1
Is there any quick way to get the desired data frame? 是否有任何快速方法来获取所需的数据帧? I tried to create a new data frame and append row by row by using ret.loc[i] = row_list, but it's too slow when the data set gets big.
我试图创建一个新的数据框并使用ret.loc [i] = row_list逐行追加,但是当数据集变大时,它太慢了。
You can use pivot_table
: 您可以使用
pivot_table
:
print df.pivot_table(index=['ID', 'Name','Sex'], columns='Date', values='Weight')
Date 2015-01-01 2015-01-02
ID Name Sex
1 Bill M 68.1 68.3
2 Mary F 45.2 45.3
3 Gary M 89.2 89.1
And if you need reset_index
use strftime
: 如果您需要
reset_index
使用strftime
:
df = df.pivot_table(index=['ID', 'Name','Sex'], columns='Date', values='Weight')
df.columns = df.columns.strftime('%Y-%m-%d')
#if you need sorting columns
df= df.sort_index(axis = 1)
df = df.reset_index()
print df
ID Name Sex 2015-01-01 2015-01-02
0 1 Bill M 68.1 68.3
1 2 Mary F 45.2 45.3
2 3 Gary M 89.2 89.1
Or: 要么:
print df.pivot_table(index=['ID', 'Name','Sex'], columns='Date', values='Weight').reset_index()
Date ID Name Sex 2015-01-01 00:00:00 2015-01-02 00:00:00
0 1 Bill M 68.1 68.3
1 2 Mary F 45.2 45.3
2 3 Gary M 89.2 89.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.