【问题标题】:Can't convert Pandas DataFrame series into an integer? [duplicate]无法将 Pandas DataFrame 系列转换为整数? [复制]
【发布时间】: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)].

标签: python pandas dataframe


【解决方案1】:

df = df.astype('int32')

或者可以具体列


df['column'].astype('int32')

【讨论】:

    猜你喜欢
    • 2015-08-08
    • 2019-11-28
    • 2018-02-02
    • 2015-03-23
    • 2013-11-04
    • 2022-12-16
    • 2021-03-01
    • 2017-03-17
    • 2017-03-23
    相关资源
    最近更新 更多