Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Handling Dataframes

Tags:

python

pandas

A certain dataframe, has a similar break with the one below:

import pandas as pd

df = pd.DataFrame({'name': ['John', 'Elvis', 'Gerrard', 'Pitty'],
              'age': [22,23,24,25],
              'document': [111,222,333,4444]})

How can I make a filter to return only the rows where the values ​​in the document column are only 3 digits?

like image 871
Costa.Gustavo Avatar asked Jan 01 '23 22:01

Costa.Gustavo


1 Answers

log

df.query('2 <= log10(document) < 3')

      name  age  document
0     John   22       111
1    Elvis   23       222
2  Gerrard   24       333

Expanded example

df = pd.DataFrame({
    'name': ['John', 'Elvis', 'Gerrard', 'Pitty'],
    'age': [22, 23, 24, 25],
    'document': [11, 222, 999, 1000]
})

df

      name  age  document
0     John   22        11  # 2 digit number
1    Elvis   23       222  # 3 digit number
2  Gerrard   24       999  # 3 digit number | edge case
3    Pitty   25      1000  # 4 digit number | edge case

Let's get only 3 digit numbers

df.query('2 <= log10(document) < 3')

      name  age  document
1    Elvis   23       222
2  Gerrard   24       999
like image 190
piRSquared Avatar answered Jan 13 '23 22:01

piRSquared