英文:
Want to drop all rows containing some text
问题
我想要删除包含TOTAL的所有行:
data = pandas.read_excel(r'C:\Users\lenovo\Desktop\tax reports\sales_expense_regionwiseGST.xlsx')
data2 = data2[data2.iloc[:,0].str.contains("Total").index]
以下代码用于导入数据,然后选择第一列使用iloc
或使用data['State/Union Territory']
:
使用iloc
会选择第1列"类型",而使用data['column_NAME_HERE']
会导致错误。
我想要提取这些行并删除它们。
英文:
I want to remove all rows containing TOTAL:
data=pandas.read_excel(r'C:\Users\lenovo\Desktop\tax reports\sales_expense_regionwiseGST.xlsx')
data2[data2.iloc[:,0].str.contains("Total".index)]
The following code to import and then selecting first column with iloc
or using data['State/Union Territory')
iloc
results in selecting the 1st column "type" as well, while data['column_NAME_HERE']
ends up in error.
I want to fetch the rows and delete it.
答案1
得分: 0
pandas
具有向量化的字符串操作,因此您可以只筛选出包含您不想要的字符串的行:
data[~data.<column_name>.str.contains("<your_text_here>")]
另一种方法(如果您有多个字符串要筛选):
data = data[~data['your column'].isin(['list of strings'])]
英文:
pandas
has vectorized string operations, so you can just filter out the rows that contain the string you don't want:
data[~data.<column_name>.str.contains("<your_text_here>")]
Another approach (if you have more than one string to filter):
data = data[~data['your column'].isin(['list of strings'])]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论