【发布时间】:2021-07-28 21:21:20
【问题描述】:
全部。我有一个结构如下的 Pandas 数据框:Pandas DataFrame
我想隔离行的子集,特别是那些属于我已存储在列表中的坐标范围的行。这是我目前正在使用的:
#create list of (putative?) exon coordinates
exonCoord = [(23114116, 23114327),(23117342,23117435),(23131892,23132007)]
#empty dataframe for only SNPs in coding regions
codingSNPs = pd.DataFrame(columns = ['pos','A','T','G','C','del','ins'])
for c in exonCoord:
for idx,row in SNPs.iterrows():
x = int(idx['pos'])
if c[1] < x < c[0]:
codingSNPs.append(idx,row)
在 x = int(idx['pos']) 行我收到错误,“'int' 对象不可下标。”将数据框列转换为整数的最简单方法是什么?非常感谢有关如何改进此代码的任何其他反馈。
【问题讨论】:
-
我认为您的意思是在该行使用
row而不是idx。该错误消息表明您将int视为容器,即您正在使用带有int的索引。 -
除了使用
idx而不是row,这不是选择行的正确方法。请参阅此 answer 的副本。selected = df[df['pos'].between(23114116, 23114327) | df['pos'].between(23117342,23117435) | df['pos'].between(23131892,23132007)].