gpt4 book ai didi

python - 如果某个单元格包含带有python的 "0",有没有办法在excel中删除一行?

转载 作者:行者123 更新时间:2023-12-04 20:51:44 26 4
gpt4 key购买 nike

我正在寻找一种使用 Python 编辑 Excel 设备列表的快速方法。
目前我正在查看一个设备列表,其中包含许多在数量列上包含“0”的行项目。

我想删除这些数量为“0”的行。

例子:

  from this:

货号 |设备 |数量 |价格
1    |   Pots    |  3  | 10.99 
2 | Pans | 0 | 16.99
3 | Spoons | 1 | 11.99
4 | Forks | 7 | 0.99
5 | Knives | 0 | 20.99
6 | Lids | 0 | 12.99
7 | Spatulas| 2 | 5.99
8 | Tongs | 8 | 6.99
9 | Grill | 1 | 12.99




to this:

货号 |设备 |数量 |价格
1    |   Pots    |  3  | 10.99 
3 | Spoons | 1 | 11.99
4 | Forks | 7 | 0.99
7 | Spatulas| 2 | 5.99
8 | Tongs | 8 | 6.99
9 | Grill | 1 | 12.99

(无需重新编号“项目编号”栏)

我仍在学习 Python,我知道如何使用 pandas 创建数据框,并在特定条件下删除行,但我不确定如何导入现有的 excel 文件并在特定单元格条件下删除某些行。
# Here is what I have done so far

import numpy as np

d = {
'Equipment':['Pots','Pans','Spoons','Forks','Knives','Lids',
'Spatulas','Tongs','Grill','Skewers'],
'QTY':[3,0,1,7,0,0,2,8,1,0]}

df = pd.DataFrame(d,columns=['Equipment','QTY'])

df[df.QTY != 0]

本质上,我希望开发一个脚本,我可以在其中删除数量为 0 的订单项。

最佳答案

你几乎拥有它:

import pandas as pd

df = pd.read_excel("file.xlsx")

df = df[df.QTY != 0]

df.to_excel("file.xlsx", index=False)

关于python - 如果某个单元格包含带有python的 "0",有没有办法在excel中删除一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57861183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com