在Pandas中使用索引子句的错误适当标记为’ignore’。

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

Proper notation for errors='ignore' with index clause in Pandas

问题

在另一个项目上工作今天。随着我的学习进展,情况要好得多,不过我正在尝试一些新东西。

我有一个数据框,它的性能非常出色。但是,我需要删除某些零件编号的行,因为它们与我的当前项目无关,我不想费力去筛选它们。目前表格中有大约112个这样的行,所以手动处理在我SQL数据库中的三个表格中将会非常耗时。目前我遇到了以下错误:

.....'KCB-0100T')' 是无效的键

我认为这只是意味着它在表格中不存在。它并不总是存在,也不应该存在。在处理以下代码时,错误='ignore' 的正确形式是什么?

indexdf = df[(df['Part Number'] == 'KSA-9132',....).index

再往下一点,我有以下代码:

df.drop(indexdf, errors='ignore', axis=0, inplace=True)

但似乎在它达到那一点之前就抛出了错误。

英文:

working on another project today. It's going a lot better as I'm learning more, however I'm trying something new.

I have a dataframe that is performing excellently. However, I need it to drop rows of certain part numbers, as they are unrelated to my current project and I don't want to pick through them. There's about 112 in the sheet right now, so doing it manually would be incredibly time-consuming across the three sheets that get added to my SQL database. Right now I get the error

.....'KCB-0100T')' is an invalid key

Which I believe just means it doesn't exist in the sheet. It wont all the time, and isn't supposed to. What's the proper form for errors='ignore' when it comes to

indexdf = df[(df['Part Number'] == 'KSA-9132',....).index

A Little lower I have

df.drop(indexdf, errors='ignore',axis=0,inplace=True)

But it seems to throw the error before it even gets to that point.

答案1

得分: 0

尝试使用掩码和isin函数:

# 将要删除的零件号添加到这个列表中
part_numbers_to_drop = ['KSA-9132', 'KCB-0100T', ...]

# 使用isin()创建一个布尔掩码
mask = df['零件号'].isin(part_numbers_to_drop)

# 删除掩码为True的行
df.drop(df[mask].index, inplace=True)
英文:

Try this using a mask and isin:

# Add the part numbers you want to drop to this list
part_numbers_to_drop = ['KSA-9132', 'KCB-0100T', ...] 

# Use isin() to create a boolean mask 
mask = df['Part Number'].isin(part_numbers_to_drop)

# Drop the rows the mask 
df.drop(df[mask].index, inplace=True)

huangapple
  • 本文由 发表于 2023年7月18日 02:44:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707297.html
匿名

发表评论

匿名网友

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

确定