要删除包含某些tex的所有行吗

2024-04-24 22:08:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我要删除包含以下内容的所有行:

enter image description here

data=pandas.read_excel(r'C:\Users\lenovo\Desktop\tax reports\sales_expense_regionwiseGST.xlsx')       
data2[data2.iloc[:,0].str.contains("Total".index)]

要导入的以下代码,然后使用iloc或使用数据['State/Union Territory'选择第一列

iloc也会导致选择第1列“type”,而data['column_NAME_HERE']最终会出错

我想获取行并删除它


Tags: pandasreaddataxlsxexceluserstaxreports
1条回答
网友
1楼 · 发布于 2024-04-24 22:08:55

pandas具有矢量化的字符串操作,因此您可以只筛选出包含不需要的字符串的行:

data[~data.<column_name>.str.contains("<your_text_here>")]

另一种方法(如果要筛选多个字符串):

data = data[~data['your column'].isin(['list of strings'])]

希望这有帮助。祝你好运

相关问题 更多 >