简体   繁体   English

在缺失值索引上附加一个列表

[英]Append a list on missing value index

{
"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.我有一个 600 行的数据集,我给出了第一行。

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.我想使用默认库(os、sys、time、JSON、CSV 等)而不是 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"我尝试了两个不同的版本,但出现相同的错误“JSON 对象必须是 str、字节或字节数组,而不是列表”和“列表索引必须是整数或切片,而不是 str”

Trying to perform #1尝试执行 #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尝试执行 #2

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

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:您可以使用csv.DictReader将 csv 文件中的行作为字典读取,然后遍历结果以检查空的Height值:

import csv

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM