1

Here's a sample dataframe:

df = pd.DataFrame([['A0000x', 'a'],
                    ['B0010x', 'b'],
                    ['C0020x', 'c'],
                    ['D0040x', 'd']])

df.columns = ['num', 'let']

I would like to extract only the rows where the integer comprised of 3rd and 4th character in the num column is divisible by 2, for example. So I need to check if df['num'][2:4] % 2

I got this far, but can't figure out how to convert it into an integer:

df.index[df['num'].str[2:4] == '01']

Raksha
  • 1,572
  • 2
  • 28
  • 53

2 Answers2

2

Use astype to convert the string column to int, then boolean index.

df['num'].str[2:4].astype(int)

0    0
1    1
2    2
3    4
Name: num, dtype: int64

df[df['num'].str[2:4].astype(int) % 2 == 0]

      num let
0  A0000x   a
2  C0020x   c
3  D0040x   d
cs95
  • 379,657
  • 97
  • 704
  • 746
0
import pandas as pd

df = pd.DataFrame([['A0000x', 'a'],
                    ['B0010x', 'b'],
                    ['C0020x', 'c'],
                    ['D0040x', 'd']])

df.columns = ['num', 'let']

for index, row in df.iterrows():
    if (int(row["num"][2:4]) % 2 ==0):
        print(row["num"])
sentence
  • 8,213
  • 4
  • 31
  • 40
  • 1
    [My recommendation](https://stackoverflow.com/a/55557758/4909087) is not to iterate over a dataFrame for performance reasons (it's also an antipattern). – cs95 Apr 12 '19 at 22:43
  • I definitely agree. Though, the basics of `for` loop and `if` condition could be more readable for a novice eye. – sentence Apr 12 '19 at 22:50
  • Perhaps... may not however be appropriate to assume OP is a beginner (seeing as [they've been working with pandas since August last year](https://stackoverflow.com/q/51831634/4909087)). – cs95 Apr 12 '19 at 22:54
  • 1
    Nothing personal against OP. It was a general consideration. Anyway, I agree with you about the code. – sentence Apr 12 '19 at 23:02