在Python中,在每行中检查零,如果行有3个或更多零,请删除行。当前代码对文件没有任何作用
我想连续计算零的数量。如果它具有三个或多个零,请删除行。删除了所有带有三个或更多零的行后,请导出新文件。
:
年 | 2011 | 2012 | 2013 | 2013 | 2015 | 2016 | 2017 | 2017 | 2018 | 2020 | 2021 | 2022 | $ |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Person_a | $ 10.00 | 20.00 | 20.00 | $ | $ | 20.00 $ 50.00 | $ 0.00 | 0.00 $ 0.00 | 10.00 $ | $ 0.00 | $ 50.00 | $ | 1000.00 |
CSV | $ 10.00 $ | $ | 0.00 | $ 0.00 | $ | $ | 10.00 | 10.00 | 10.00 | $ | $ | 0.00 | 所需 |
结果:
年 | 2011 | 2011 | 年2012 | 2013 | 2014 | 2015 | 2016 | 2016 | 2017 | 2018 | 2018 2020 | 2021 | 2022 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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:
Year | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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:
Year | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新
旧答案
您可以使用:
Update
Old answer
You can use: