Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate specific value in a dataframe

I have a large dataset. I am trying to read it with Pandas Dataframe. I want to separate some values from one of the columns. Assuming the name of column is "A", there are values ranging from 90 to 300. I want to separate any values between 270 to 280. I did try below code but it 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]
like image 426
user8034918 Avatar asked Dec 24 '22 17:12

user8034918


2 Answers

Use between with 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
like image 123
jezrael Avatar answered Dec 26 '22 19:12

jezrael


Using numpy to speed things up and reconstruct a new dataframe.
Assuming we use jezrael's 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
like image 28
piRSquared Avatar answered Dec 26 '22 18:12

piRSquared