我正在写一个脚本来标准化RT-PCR的数据。我正在从tsv文件中读取数据,我正在努力将其放入pandas数据框中,以便使其可用。这里的问题是行索引具有相同的名称,是否有可能使其成为分层结构?
我使用的是Python 3.6。我已经尝试了.groupby()和.pivot(),但是我似乎不能让它做我想要的事情。
def calculate_peaks(file_path):
peaks_tsv = pd.read_csv(file_path, sep='\t', header=0, index_col=0)我的输入文件是:input file image
我的预期输出:
EMB.brep1.peak EMB.brep1.length EMB.brep2.peak EMB.brep2.length EMB.brep3.peak EMB.brep3.length
primer name
Hv161 0 19276 218.41 20947 218.39 21803 218.26
1 22906 221.35 26317 221.17 26787 221.21
Hv223 0 4100 305.24 5247 305.37 4885 305.25
1 2593 435.25 3035 435.30 2819 435.32
2 4864 597.40 5286 597.20 4965 596.60实际输出:
EMB.brep1.peak EMB.brep1.length EMB.brep2.peak EMB.brep2.length EMB.brep3.peak EMB.brep3.length
primer name
Hv161 19276 218.41 20947 218.39 21803 218.26
Hv161 22906 221.35 26317 221.17 26787 221.21
Hv223 4100 305.24 5247 305.37 4885 305.25
Hv223 2593 435.25 3035 435.30 2819 435.32
Hv223 4864 597.40 5286 597.20 4965 596.60发布于 2019-08-02 00:07:15
您可以这样做:
peaks_tsv = pd.read_csv(file_path, sep='\t', header=0)
peaks_tsv['idx'] = peaks_tsv.groupby('primer name').cumcount()
peaks_tsv.set_index(['primer name', 'idx'], inplace=True)https://stackoverflow.com/questions/57312974
复制相似问题