Reputation: 326
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)
Upvotes: 1
Views: 260
Reputation: 863541
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]
Upvotes: 1