1

I have an initial csv file that contains certian values that I am interested in, and I would like to read it and to filter it at the same time.

The file has the following structure:

A B
25 xx
NaN yy
32 zz
25 zz

What I normally do is read it first and then, apply the filter:

df = pd.read_csv(filename, sep=";")
df = df[df['A']==25]

I would like to know if it is possible to filter it in a chained way such as the following:

df = pd.read_csv(filename, sep=";")\
.where('A'==25)
Javier Monsalve
  • 326
  • 3
  • 14

1 Answers1

1

If need remove missing rows use DataFrame.query with trick for compare if A is same like A, because np.nan != np.nan:

df = pd.read_csv(filename, sep=";").query('A==A')

Or use selection by callable:

df = pd.read_csv(filename, sep=";").loc[lambda x: x.A.notna()]

If need test another values:

df = pd.read_csv(filename, sep=";").query('A==25')

Or:

df = pd.read_csv(filename, sep=";").loc[lambda x: x.A == 25]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252