-2

all. I have a Pandas dataframe that is structured like this: Pandas DataFrame

I want to isolate a subset of rows, specifically those that fall into a range of coordinates, which I have stored in a list. This is what I am currently working with:

#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)

On line x = int(idx['pos']) I receive the error, "'int' object is not subscriptable." What is the easiest way to convert the dataframe columns into integers? Any other feedback on how to improve this code is much appreciated.

Sarah
  • 1
  • 1
    I think you meant to use `row` instead of `idx` on that line. That error message is saying that you're treating `int` as container i.e. you're using indexing with `int`. – nobleknight May 06 '21 at 14:34
  • Aside from using `idx` instead of `row`, this is not the correct way to select rows. See this [answer](https://stackoverflow.com/a/40442778/7758804) of the duplicate. `selected = df[df['pos'].between(23114116, 23114327) | df['pos'].between(23117342,23117435) | df['pos'].between(23131892,23132007)]`. – Trenton McKinney May 09 '21 at 17:58

1 Answers1

0

df = df.astype('int32')

or could specific column


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

Emerson Pedroso
  • 181
  • 3
  • 13