我有一个与此类似的 DataFrame:
Chr Start_Position End_Position Type
1 10000 10001 SNP
5 45321 45327 INS
12 44700 44710 DEL
我需要根据以下内容更改某些单元格的值Type
:
SNP
需要Start_Position
+ 1INS
需要End_Position
+ 1DEL
需要Start_Position
+ 1我的问题是我目前的解决方案非常冗长。我尝试过的(dataframe
是原始数据源):
snp_records = dataframe.loc[dataframe["Type"] == "SNP", :]
del_records = dataframe.loc[dataframe["Type"] == "DEL", :]
ins_records = dataframe.loc[dataframe["Type"] == "INS", :]
snp_records.loc[:, "Start_Position"] = snp_records["Start_Position"].add(1)
del_records.loc[:, "Start_Position"] = del_records["Start_Position"].add(1)
ins_records.loc[:, "End_Position"] = ins_records["End_Position"].add(1)
dataframe.loc[snp_records.index, "Start_Position"] = snp_records["Start_Position"]
dataframe.loc[del_records.index, "Start_Position"] = del_records["Start_Position"]
dataframe.loc[ins_records.index, "End_Position"] = ins_records["End_Position"]
由于我必须为比示例更多的列执行此操作(不过,类似的概念)这变得非常冗长,并且可能容易出错(事实上,由于所有重复,我在输入示例时犯了几个错误)线。
这个问题与我的相似,但那里的值是预定义的,而我需要自己从数据中获取它们。
你可以这样做:
df.loc[df['Type'].isin(['SNP','INS']), 'Start_Position'] += 1
df.loc[df['Type'].eq('INS'), 'End_Position'] += 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句