在python中,如何删除特定列为空的整行?

2024-09-21 05:37:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试找出如何使下面的代码排除第[9]行有空条目的任何实例。当前csv文件为每一行输出,这将导致行中的“Dad”项为空。我希望输出csv文件不包括任何“Dad”列(第[9]行)为空的行。。。你知道吗

非常感谢您的帮助!你知道吗

def guardiansfather():
with open('hallpass_raw.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    with open('outputfiles/guardians_father.csv', mode='w', newline='') as output_file:
        write = csv.writer(output_file, delimiter=',', quoting=csv.QUOTE_MINIMAL)
        for row in csv_reader:
            a = row[0]
            b = row[1]
            c = row[2]
            studentnumber = row[3]
            firstname = row[4]
            middlename = row[5]
            lastname = row[6]
            teacher = row[7]
            idnumber = row[8]
            father = row[9]
            mother = row[10]
            guardianemail = row[11]
            phone = row[12]
            fatheremail = row[13]
            motheremail = row[14]
            guardianphone = row[15]
            schoolname = 'NAME OF SCHOOL'
            relationship = 'Father'
            father_first = father.split(sep=','[0])
            father_last = father.split(sep=', '[1])


            write.writerow([schoolname, studentnumber, father_first, father_last, relationship, phone, fatheremail])

Tags: 文件csvoutputaswithphoneopenreader
2条回答

使用if语句

def guardiansfather():
    with open('hallpass_raw.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        with open('outputfiles/guardians_father.csv', mode='w', newline='') as output_file:
            write = csv.writer(output_file, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            for row in csv_reader:
                a = row[0]
                b = row[1]
                c = row[2]
                studentnumber = row[3]
                firstname = row[4]
                middlename = row[5]
                lastname = row[6]
                teacher = row[7]
                idnumber = row[8]
                father = row[9]

                # Skip rows with empty father
                if father.strip() == '':
                    continue

                mother = row[10]
                guardianemail = row[11]
                phone = row[12]
                fatheremail = row[13]
                motheremail = row[14]
                guardianphone = row[15]
                schoolname = 'NAME OF SCHOOL'
                relationship = 'Father'
                father_first = father.split(sep=','[0])
                father_last = father.split(sep=', '[1])

                write.writerow([schoolname, studentnumber, father_first, father_last, relationship, phone, fatheremail])

您可以使用pandas库:

1.)将csv读入数据框:

import pandas as pd
df = pd.read_csv('hallpass_raw.csv', sep=',')

示例:假设您的数据帧是这样的。你知道吗

In [365]: df
Out[365]: 
  fname   age  salary
0     a   5.0     1.5
1     a   5.0     1.5
2     b           1.0
3     b  15.0     
4     c  20.0     1.0

2.)删除特定列为NULL的行:

要删除在列age中具有NULL值的行:

In [364]: df = df[df.age <> '']
Out[364]: 
  fname   age  salary
0     a   5.0     1.5
1     a   5.0     1.5
3     b  15.0     
4     c  20.0     1.0

你可以看到第二排被放下了。你知道吗

3.)将处理后的数据帧写回csv:

df.to_csv('new.csv', index=False)

这样,您就不必担心处理csv的复杂循环。你知道吗

相关问题 更多 >

    热门问题