Reputation: 1142
This is example of the data I have
1, "dep, anxiety", 30
2, "dep" , 40
4, "stress" , 30
7, "dep, fobia" , 20
I want to use pandas to filter rows having "dep" and save it in a new cvs file. output should be:
1, "dep, anxiety", 30
7, "dep, fobia" , 20
2, "dep" , 40
this is my code:
import pandas as pd
patients = pd.read_csv("patients.csv", encoding='latin-1')
print(patients["dep" in patients["qual"]])
that has the following error
"return self._engine.get_loc(self._maybe_cast_indexer(key))"
And I do not know how to export the extracted data to new csv file.
Upvotes: 0
Views: 2988
Reputation: 210882
you can do it this way:
In [213]: patients
Out[213]:
ID dis rank
0 1 dep, anxiety 30
1 2 dep 40
2 4 stress 30
3 7 dep, fobia 20
In [214]: patients[(patients['dis'].str.contains('dep')) & (patients['rank'] == 30)]
Out[214]:
ID dis rank
0 1 dep, anxiety 30
PS rank
is a pandas method, so you can't use dot-column (df.column_name
) accessor, because pandas will think you are calling the NDFrame.rank
method:
Demo:
Here we call a reference to the NDFrame.rank
method:
In [215]: patients.rank
Out[215]:
<bound method NDFrame.rank of ID dis rank
0 1 dep, anxiety 30
1 2 dep 40
2 4 stress 30
3 7 dep, fobia 20>
Here we call rank
column:
In [216]: patients['rank']
Out[216]:
0 30
1 40
2 30
3 20
Name: rank, dtype: int64
Upvotes: 1