在Python中,在每行中检查零,如果行有3个或更多零,请删除行。当前代码对文件没有任何作用

发布于 2025-01-22 00:34:30 字数 3906 浏览 2 评论 0原文

我想连续计算零的数量。如果它具有三个或多个零,请删除行。删除了所有带有三个或更多零的行后,请导出新文件。

201120122013201320152016201720172018202020212022$
Person_a$ 10.0020.0020.00$$20.00 $ 50.00$ 0.000.00 $ 0.0010.00 $$ 0.00$ 50.00$1000.00
CSV$ 10.00 $$0.00$ 0.00$$10.0010.0010.00$$0.00所需

结果:

20112011年201220132014201520162016201720182018 202020212022
Person_b$ 100.00$ 150.00$ 1.00$ 50.00$ 0.25$ 0.25 $0.25 $ 0.00 $ 50.00$ 50.00$ 50.00 $ 60.00$ 50.00 $0.00 $ 0.00$ 0.00$ 1000.00

当前代码对文件无济于事。我认为我认为零是计数零,如果超过3列行,但是file.csv和newfile.csv的行计数是相同的:

import pandas as pd

df = pd.read_csv("C:/Users/File.CSV" , encoding = "ISO-8859-1") # import csv as DataFrame

df_new = df.loc[df.eq(0).sum(1).le(3),] # Look for zeros, if more than 3, remove row

df_new.to_csv( "C:/Users/Folder/NewFile.CSV", index=False ) # Export new file

我也尝试过,但是再次没有更改文件:


df = pd.read_csv("C:/Users/File.CSV" , encoding = "ISO-8859-1") # import csv as DataFrame

df_new = df[df.eq('$0.00').sum(1) <= 3] # Look for zeros, if more than 3 remove row

df_new.to_csv( "C:/Users/Folder/NewFile.CSV", index=False ) # Export new file

I'm looking to count the amount of zeros in a row. If it has three or more zeros, remove the row. Once all rows with three or more zeros are removed, export the new file.

CSV:

Year2010201120122013201420152016201720182019202020212022
Person_A$10.00$20.00$20.00$50.00$0.00$10.00$0.00$0.00$50.00$0.00$10.00$0.00$1.00
Person_B$100.00$150.00$1.00$50.00$0.25$100.00$0.00$50.00$60.00$50.00$0.00$0.00$1000.00

Desired result:

Year2010201120122013201420152016201720182019202020212022
Person_B$100.00$150.00$1.00$50.00$0.25$100.00$0.00$50.00$60.00$50.00$0.00$0.00$1000.00

Current Code does nothing to the file. I have what I think is count zeros, if more than 3 drop the row, but the row count for File.csv and NewFile.csv are the same:

import pandas as pd

df = pd.read_csv("C:/Users/File.CSV" , encoding = "ISO-8859-1") # import csv as DataFrame

df_new = df.loc[df.eq(0).sum(1).le(3),] # Look for zeros, if more than 3, remove row

df_new.to_csv( "C:/Users/Folder/NewFile.CSV", index=False ) # Export new file

I have also attempted this, but again makes no changes to File:


df = pd.read_csv("C:/Users/File.CSV" , encoding = "ISO-8859-1") # import csv as DataFrame

df_new = df[df.eq('$0.00').sum(1) <= 3] # Look for zeros, if more than 3 remove row

df_new.to_csv( "C:/Users/Folder/NewFile.CSV", index=False ) # Export new file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2025-01-29 00:34:30

更新

df = pd.read_csv('GiftYearTotal.csv', encoding='ISO-8859-1')
df = df.apply(lambda x: x.str.strip())
out = df[df.eq('$0.00').sum(1) <= 3]

旧答案

您可以使用:

out = df[df.eq('$0.00').sum(1) <= 3]
print(out)

# Output
       Year     2010     2011   2012    2013   2014     2015   2016    2017    2018    2019   2020   2021      2022
1  Person_B  $100.00  $150.00  $1.00  $50.00  $0.25  $100.00  $0.00  $50.00  $60.00  $50.00  $0.00  $0.00  $1000.00

Update

df = pd.read_csv('GiftYearTotal.csv', encoding='ISO-8859-1')
df = df.apply(lambda x: x.str.strip())
out = df[df.eq('$0.00').sum(1) <= 3]

Old answer

You can use:

out = df[df.eq('$0.00').sum(1) <= 3]
print(out)

# Output
       Year     2010     2011   2012    2013   2014     2015   2016    2017    2018    2019   2020   2021      2022
1  Person_B  $100.00  $150.00  $1.00  $50.00  $0.25  $100.00  $0.00  $50.00  $60.00  $50.00  $0.00  $0.00  $1000.00
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文