Extracting data using pandas from a CSV file with a special condition
这是我拥有的数据的示例
1 2 3 4 | 1,"dep, anxiety", 30 2,"dep" , 40 4,"stress" , 30 7,"dep, fobia" , 20 |
我想使用pandas过滤具有"dep"的行,并将其保存到新的cvs文件中。输出应为:
1 2 3 | 1,"dep, anxiety", 30 7,"dep, fobia" , 20 2,"dep" , 40 |
这是我的代码:
1 2 3 4 | import pandas as pd patients = pd.read_csv("patients.csv", encoding='latin-1') print(patients["dep" in patients["qual"]]) |
有以下错误
1 | "return self._engine.get_loc(self._maybe_cast_indexer(key))" |
我不知道如何将提取的数据导出到新的csv文件中。
你可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 | 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
演示:
这里我们称为
1 2 3 4 5 6 7 | 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> |
这里我们称为
1 2 3 4 5 6 7 | In [216]: patients['rank'] Out[216]: 0 30 1 40 2 30 3 20 Name: rank, dtype: int64 |