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

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

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.neDataFrame.any

df = pd.read_csv("file-url")

df = df[df.iloc[:, -2:].ne(0).any(axis=1)]

另一种方法是通过使用~来筛选反向掩码,检查两列是否都不为0,使用DataFrame.eqDataFrame.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')

huangapple
  • 本文由 发表于 2023年5月25日 14:27:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329453.html
匿名

发表评论

匿名网友

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

确定