How to separate data based on null values in two different CSV files in python

564 Views Asked by At

I am new to python development and and I am trying to separate csv file into two different text files based on null values

my csv file has data like

enter image description here

and

enter image description here

My csv file contains four fields facility, Truck, Driver and licences truck and driver has some of the null values I want to create two separate files for row values where truck value us null and another file will contain information where driver value is null.

I tried the following code but it is not eliminating null values it shows either 0 or space in text file

    License = pd.read_csv("E:\ActiveCityLicenses.csv")

    a=License.isnull().sum()
    print(a)
    print(License.shape)
    m=License[License['TRUCK_ID'].isnull()]
    print(m)
    n=License.dropna(axis= 0, subset= ['TRUCK_ID'], inplace=True)
    print(n)

    License.to_csv(r'E:\DriverLicense.txt', header=None, index=None, mode='w', columns=None)
    #I had to create two data frames as after doing first dorpna entire frame gets empty


    License1 = pd.read_csv("E:\ActiveCityLicenses.csv")
    p=License1.dropna(axis= 0, subset= ['EMPLOYEE_ID'], inplace=True)
    print(p)

    License1.to_csv(r'E:\TruckLicense.txt', header=None, index=None, sep=',', mode='w')

Can anyone please suggest a better approach of doing it, or what I am missing over here? output in the text file is

     A119,BF01,,TOR|MARK|BRAM|MISS|RHILL|VAU
     A119,BF03,,TOR|MARK|BRAM|MISS|RHILL|VAU
     A119,BF04,,TOR|MARK|BRAM|MISS|RHILL|VAU
     A119,BF05,,TOR|MARK|BRAM|MISS|RHILL|VAU

space should not be there.

2

There are 2 best solutions below

0
user3465377 On BEST ANSWER
df= df[pd.notnull(df['TRUCK_ID'])]
0
Michielver On

If you want to make sure the empty column is not there, you can always remove the column by doing for example: License.drop(labels="EMPLOYEE_ID", axis=1, inplace=True) I am not entirely sure where you want which column removed, so I cannot give a more complete solution.