Home > Back-end >  Append a list on missing value index
Append a list on missing value index

Time:11-16

{
"First Name": "Jonathan",
"Last Name": "Thomas",
"Marital Status": "married or civil partner",
"Sex": "Male",
"Age (Years)": 46,
"Height": 160,
}

I have a 600-row data set which I gave the first row.

import pandas as pd
df = pd.read_csv("user_data.csv")

df[df["Height"].isnull()].index.tolist()

On the code above I have used pandas. I want to re-write that code with the same logic using default libraries.

Logic is: searching elements on the column and writing the missing one into the empty list

I want to use default libraries (os, sys, time, JSON, CSV, …) instead of pandas. Could you help and guide me convert that code?

I tried two different versions but I get the same errors "the JSON object must be str, bytes or byte array, not list" and "list indices must be integers or slices, not str"

Trying to perform #1

missing_age_indexes = [idx for idx, obj in enumerate(l['Height']) if obj.get('Height', None) is not None]
missing_age_objects = [l[idx] for idx in missing_age_indexes]
print(missing_age_indexes)

Trying to perform #2

for i, j in enumerate(l["Dependants"]):
    if j == None:
        print(i)

CodePudding user response:

You can read the lines in a csv file as dictionaries with csv.DictReader, then iterate over the results to check for empty Height values:

import csv

reader = csv.DictReader(open('user_data.csv'))
missing_age_indexes = [idx for idx, row in enumerate(reader) if not row['Height']]
  • Related