Single value in data frame

I have a large dataset. I am trying to read it using a Pandas Dataframe. I want to highlight some values ​​from one of the columns. Assuming the column name is "A", there are values ​​from 90 to 300. I want to separate any values ​​from 270 to 280. I tried the code below, but this is wrong!

%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('....csv')
df2 = df[ 270 < df['A'] < 280]
+4
source share
3 answers

Use betweenwith boolean indexing:

df = pd.DataFrame({'A':range(90,300)})

df2 = df[df['A'].between(270,280, inclusive=False)]
print (df2)
      A
181  271
182  272
183  273
184  274
185  275
186  276
187  277
188  278
189  279

Or:

df2 = df[(df['A'] > 270) & (df['A'] < 280)]
print (df2)
      A
181  271
182  272
183  273
184  274
185  275
186  276
187  277
188  278
189  279
+5
source

Use numpyto speed up and restore a new data frame.
Assuming we are using jezrael sample data

a = df.A.values
m = (a > 270) & (a < 280) 
pd.DataFrame(a[m], df.index[m], df.columns)

       A
181  271
182  272
183  273
184  274
185  275
186  276
187  277
188  278
189  279
+3
source

query() :

df2 = df.query("270 < A < 280")

:

In [40]: df = pd.DataFrame({'A':range(90,300)})

In [41]: df.query("270 < A < 280")
Out[41]:
       A
181  271
182  272
183  273
184  274
185  275
186  276
187  277
188  278
189  279
+2

Source: https://habr.com/ru/post/1678071/


All Articles