簡體   English   中英

在python中,如何刪除特定列為空白的整行?

[英]In python how can I remove entire rows where a specific column is blank?

我試圖弄清楚如何使下面的代碼排除row [9]具有空白條目的所有實例。 當前,csv文件為每個行輸出,這導致行中帶有“ Dad”的空白條目。 我希望輸出的csv文件不包含“爸爸”列(行[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])

使用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讀入pandas數據框:

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的復雜循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM