Python 读取csv文件并检查特定列是否为负数。如果为true,则将相应的行写入另一个csv
所以我在读一个csv文件,有一个特定的列,我在检查它的值是否为负值。如果为负数,我想写入另一个csv 输入csv文件: 编号(列名): 1. -2 3. -十, 预期产出: 编号: -2 -十, 基本上,应该只将这些行写入数字列值为负值的另一个csv 我尝试过使用pandas和csv阅读器: 熊猫:Python 读取csv文件并检查特定列是否为负数。如果为true,则将相应的行写入另一个csv,python,csv,Python,Csv,所以我在读一个csv文件,有一个特定的列,我在检查它的值是否为负值。如果为负数,我想写入另一个csv 输入csv文件: 编号(列名): 1. -2 3. -十, 预期产出: 编号: -2 -十, 基本上,应该只将这些行写入数字列值为负值的另一个csv 我尝试过使用pandas和csv阅读器: 熊猫: df1=pd.read\u csv(inputCSVFile) df2=df1[df1[Attribute]在检查文件中的post输入数据后,不需要写入“==True”,预期的输出是broken@h
df1=pd.read\u csv(inputCSVFile)
df2=df1[df1[Attribute]在检查文件中的post输入数据后,不需要写入“==True”,预期的输出是broken@h4z3-在实际代码中,标识是正确的。但即使这样,输出也不正确抱歉,csv文件中的数字没有垂直对齐是的,我不关心您的“实际代码”。如果我们看不到什么在哪里,我们就帮不了你,因为这不是同一件事。嗨,astype(float)需要什么?因为我不想将值转换为浮点值,所以只想检查它们是否为负或notAstype float用作备份度量值,以防read_csv将列读取为字符串。您可以跳过此步骤,代码将给出一个结果。在代码中只需更改df2=df1[df1[Attribute]
df1=pd.read_csv(inputCSVFile)
df2=df1[df1[Attribute]<(0) == True]
df2.to_csv(outputCSVFile,index=False)
with open(inputCSVFile,"r") as inputCSV:
input=csv.reader(inputCSV)
inputList=list(input)
with open(outputCSVFile,"w") as outputCSV:
output=csv.writer(outputCSV)
indexNumber=inputList[0].index(Attribute)
print(indexNumber)
for row in input:
if row[Attribute]<0:
output.writerow(row)
import pandas as pd
df1=pd.read_csv(inputCSVFile)
df1[Attribute]=df1[Attribute].astype(float)
df2=df1[df1[Attribute]<0]
df2.to_csv(outputCSVFile,index=False)