我有一个DataFrame,有时有一个称为(UnSpecified)的行,有时却没有。当我尝试按名称删除该行且该行不存在时,出现“(未在轴中找到)”错误。

### sales 2018 ###

sales_2018 = pd.read_csv('sales 2018.csv')

# gross sales

gross_sales_2018_pivot = pd.DataFrame(pd.pivot_table(sales_2018, values=['GROSS SALES'], index=['DAYPARTNAME'], columns=['DAY OF WEEK'], aggfunc=np.sum))

gross_sales_2018_pivot = gross_sales_2018_pivot.drop('(UnSpecified)', axis=0)

gross_sales_2018_pivot.to_excel(writer, sheet_name='overall')
writer.save()


如果没有(未指定)行,如何创建一个循环使其忽略/传递错误?

最佳答案

the documentation

gross_sales_2018_pivot = gross_sales_2018_pivot.drop('(UnSpecified)', axis=0, errors='ignore')

07-27 19:42