如何忽略CSV文件中具有特定值的行?

时间:2019-02-19 15:58:52

标签: csv

我正在尝试读取CSV文件并使用感兴趣的数据创建一个新文件。在某些行中,特定数据值(按年龄和性别列出)标记为-1,因此在新的CSV表中不需要。我应该使用Pandas库重写它吗?而且,我试图忽略先前的ID(因为某些行将被忽略),并将新的行数作为新的ID。

import csv

data = []

def transform_row(row):
    # id = new count
    age = line[2]
    gender = line[3]
    url = line[4]

    return [
    #new count
    age,
    gender,
    url
    ]

# read csv file line by line
with open('data_sample.csv', 'r') as f:
    reader = csv.reader(f)

    """ bad try at ignoring the line with value -1
    for value in reader:
        if value == '-1':
            pass
        else:
            continue
    """

    # loop through each line in csv and transform
    for line in reader:
            data.append(transform_row(line))

# write a new csv file
with open('data_test.csv', 'w', newline='') as f:
    # define new csv writer
    writer = csv.writer(f, delimiter=',')

    # write a header row to our output.csv file
    writer.writerow([
        #'id', - new line count as id
        'age',
        'gender',
        'url'
    ])

    # write our data to the file
    writer.writerows(data)

也欢迎其他任何建议。

2 个答案:

答案 0 :(得分:0)

使用pandas将使您的生活更加轻松,因为csv模块不适用于精细的数据操作。如果要基于特定列的值删除行,则可以将原始csv初始化为数据框,并仅使用所需的值创建一个新的csv:

import pandas as pd

start_data = pd.read_csv('./data_sample.csv')
# replace 'age' with 'gender' if that's what you prefer
clean_data = start_data[start_data['age'] != -1]

检查start_dataclean_data的长度应显示所有不需要的行均已删除。然后,您可以使用以下命令创建新的csv:

clean_data.to_csv('./data_test.csv')

答案 1 :(得分:0)

我通过熊猫重写了脚本。这是解决该问题的两种方法。

import pandas as pd

cols = [2, 3, 4]
data = pd.read_csv('data_sample.csv', usecols=cols, header=None) data.columns = ["url", "gender", "age"]

#remove the unneeded coloumns
data = data[data['gender'] != -1]  
data = data[data['age'] != -1]

#reset the index
data.reset_index(drop=True, inplace=True)

""" Additional working solution 
indexGender = data[data['gender'] == -1].index 
indexAge = data[data['age'] == -1].index

# Delete the rows indexes from dataFrame 
data.drop(indexGender,inplace=True) 
data.drop(indexAge, inplace=True)
"""
data.to_csv('data_test.csv')

希望它将对某人有所帮助。