英文:
Working with csv file in pandas to create a new csv file from old csv file after checking if values of two fields are not zero
问题
我正在开发一个程序,其中一个CSV文件是我使用pandas从网页下载的。
import pandas as pd
df = pd.read_csv("file-url")
这个文件有5列。我需要检查这个文件的最后2列,如果它们的值都是零,那么这一行将从文件中删除。我对pandas和Python都不太熟悉。如何比较两列并使用pandas将行写入CSV文件?
英文:
I am working on a program where one csv file I am downloading from webpage using pandas
import pandas as pd
df = pd.read_csv("file-url")
This file has 5 cloumns. I need to check last 2 columns of this file if both values are zero than that line will be eliminated from file. I am new to pandas and python. How to compare two columns and how to write row into a csv file using pandas
答案1
得分: 1
使用DataFrame.iloc
选择最后两行,并通过boolean indexing
筛选,如果两列都至少有一个非0
值,则使用DataFrame.ne
和DataFrame.any
:
df = pd.read_csv("file-url")
df = df[df.iloc[:, -2:].ne(0).any(axis=1)]
另一种方法是通过使用~
来筛选反向掩码,检查两列是否都不为0
,使用DataFrame.eq
和DataFrame.all
:
df = df[~df.iloc[:, -2:].eq(0).all(axis=1)]
最后写入文件:
df.to_csv('new_file.csv')
英文:
IIUC use DataFrame.iloc
for select last 2 rows and filter by boolean indexing
if both columns has at least one non 0
value by DataFrame.ne
with DataFrame.any
:
df = pd.read_csv("file-url")
df = df[df.iloc[:, -2:].ne(0).any(axis=1)]
Alternative is filter by inverse mask by ~
by test if not both columns has 0
by DataFrame.eq
and DataFrame.all
:
df = df[~df.iloc[:, -2:].eq(0).all(axis=1)]
Last write to file:
df.to_csv('new_file.csv')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论