熊猫groupby()重新排列列

时间:2019-08-29 08:27:17

标签: pandas pandas-groupby

我想重新排列我的Excel文件:

enter image description here

成为:

enter image description here

认为可以用pandas groupby()完成

infile = pd.DataFrame(pd.read_excel("test.xlsx", header = 0))

# Extract the header and create new data frame
sample_list = infile["Sample"].drop_duplicates().tolist()
output = pd.DataFrame(columns=sample_list)

# Adding data group wise. This doesn't really do the trick though...
groups = infile.groupby("Sample")
for sample in range(len(sample_list)):
    output = output.append(groups.get_group(sample_list[sample])["Type"], ignore_index=True)

0 个答案:

没有答案