想要删除包含特定文本的所有行。

huangapple go评论91阅读模式
英文:

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.&lt;column_name&gt;.str.contains(&quot;&lt;your_text_here&gt;&quot;)]

Another approach (if you have more than one string to filter):

data = data[~data[&#39;your column&#39;].isin([&#39;list of strings&#39;])]

huangapple
  • 本文由 发表于 2020年1月3日 13:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定