我有一个像这样的数据框
Head CHR Start End Trans Num
A 1 29554 30039 ENST473358 1
A 1 30564 30667 ENST473358 2
A 1 30976 31097 ENST473358 3
B 1 36091 35267 ENST417324 1
B 1 35491 34544 ENST417324 2
B 1 35184 35711 ENST417324 3
B 1 36083 35235 ENST461467 1
B 1 35491 120765 ENST461467 2
我需要改变列的开始和结束相对于列Trans和Num。意思是,列Trans有重复的值,这在列Num中提到过,以此类推。意思是我想把所有行的Start改为-End+10,End改为-Start from next row(具有相同的Trans)-10,依此类推
Head CHR Start End Trans Num
A 1 30564 30667 ENST473358 1
A 1 30976 31097 ENST473358 2
A 1 30267 NA ENST473358 3
B 1 35277 35481 ENST417324 1
B 1 34554 35174 ENST417324 2
B 1 35721 NA ENST417324 3
B 1 35245 35481 ENST461467 1
B 1 120775 NA ENST461467 2
任何帮助是非常感谢我可以做它没有考虑与以下脚本转换,但我不会得到我想要的输出。你知道吗
start = df['Start'].copy()
df['Start'] = df.End + 10
df['End'] = ((start.shift(-1) - 10))
df.iloc[-1, df.columns.get_loc('Start')] = ''
df.iloc[-1, df.columns.get_loc('End')] = ''
print (df)
您可能需要考虑根据您希望如何利用数据来重新编制数据索引。你知道吗
您可以基于列“Trans”和“Num”索引数据,如下所示:
接下来,我们将获取每个唯一的索引,这样我们就可以将它们全部替换掉(我非常确定这部分和下面的迭代可以批量完成,但我只是很快就完成了。如果您有效率问题,请研究如何不在所有索引上循环。)
然后我们可以迭代并应用你想要的。你知道吗
运行数据时得到的结果是:
我用来生成测试用例的完整代码如下:
您可以将现有代码放入函数中,然后按
Trans
分组并应用函数:结果:
相关问题 更多 >
编程相关推荐