【发布时间】:2018-08-03 04:59:57
【问题描述】:
用于删除某些行的电子表格。
在保存到新的电子表格之前,将删除其第一列中包含以“36”开头的值的所有行。
我使用这些代码(之后需要在 Excel 中拆分列)。示例如下所示:
import xlwt
from xlrd import open_workbook
old_file = open_workbook('C:\\original.xlsx')
old_sheet = old_file.sheet_by_index(0)
new_file = xlwt.Workbook(encoding='utf-8', style_compression = 0)
new_sheet = new_file.add_sheet('Sheet1', cell_overwrite_ok = True)
contents = []
for row in range(old_sheet.nrows):
a = str(old_sheet.cell(row,0).value)
b = str(old_sheet.cell(row,1).value)
if not a.startswith("36"):
contents.append(a + "," + b)
for c, content in enumerate(contents):
new_sheet.write(c, 0, content)
new_file.save('C:\\result.xls')
这还不够,所以我想学习 Pandas 这样做的方式。
我尝试了类似 df.drop(["3649"]) 但它不起作用。
Pandas 删除行的正确方法是什么?谢谢。
【问题讨论】:
标签: python excel pandas dataframe