Home > Software engineering >  CSV - display rows in which specified column is empty
CSV - display rows in which specified column is empty

Time:10-12

i have an csv file that goes like this:

department_id,department_name,manager_id,location_id
 10,Administration,200,1700
 20,Marketing,201,1800
 30,Purchasing,114,1700
 40,Human Resources,NaN,2400
 50,Shipping,NaN,1500
......

the task is to print rows in which column manager_id is empty, and no headers are displayed.

The closest approach i achieved is :

import pandas as pd
df = pd.read_csv ('data.csv', header=None, index_col = 0 )
null_data = df[df.isnull().any(axis=1)]
print(null_data)

but it prints weird

Your code output:
1    2     3
0                                   
120              Treasury  NaN  1700
....

at the beggining. I would be grateful with any help, not really with pandas, all approaches would be great.

CodePudding user response:

To get the rows where manager_id is empty, we can do df[df['manager_id'].isnull()]

CodePudding user response:

First of all, your way of reading the CSV file with header=None assume no column labels. Hence, your printout shows only columns in numbers.

You can change the code reading the CSV to:

df = pd.read_csv ('data.csv')

Then, to select rows where manager_id is empty, you can use:

null_data = df.loc[df['manager_id'].isna()]
print(null_data)

Result:

   department_id  department_name  manager_id  location_id
3             40  Human Resources         NaN         2400
4             50         Shipping         NaN         1500
  • Related