如何以熊猫为中心?我无法解决“重复条目”错误。输入和输出应如下所示。
import pandas as pd
input = pd.DataFrame({'measure': ['length','length','length','weight','weight','weight','sex','sex','sex'],
'species': [10, 10, 10, 10, 10, 10, 10, 10, 10],
'value': [1, 2, 3, 13, 45, 123, 0, 1, 1],
'set': [3, 3, 3, 3, 3, 3, 3, 3, 3]})
output = pd.DataFrame({'set': [3,3,3],
'species': [10, 10, 10],
'length': [1, 2, 3],
'weight': [13, 45, 123],
'sex': [0, 1, 1]})
test = input.pivot(index='set',columns='measure', values='value')
print(test)
答案 0 :(得分:5)
在这种情况下,我们通常解析为groupby().cumcount()
以获取新索引:
indf['idx'] = indf.groupby('measure').cumcount()
(indf.pivot_table(index=['idx','species','set'],
columns='measure',
values='value')
.reset_index(('species','set'))
)
输出:
measure species set length sex weight
idx
0 10 3 1 0 13
1 10 3 2 1 45
2 10 3 3 1 123