简体   繁体   English

在搜索 CSV 文件列时获取列表中的缺失值索引写入

[英]Get missing values index write on a list while searching on CSV file Column

Hello Everyone,大家好,

{
"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 that I write using the pandas library.在我使用 Pandas 库编写的代码上。 I want to re-write that code with the same logic code 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 that I used on mentioned code.我想使用默认库(os、sys、time、JSON、CSV 等)而不是我在提到的代码中使用的 pandas。 Could you help and guide me convert that code?你能帮助和指导我转换那个代码吗?

Assuming l is a list of JSON objects like the one you provided, this should work:假设l是您提供的 JSON 对象列表,这应该可以工作:

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

To get a list of the objects from that, this'll work:要从中获取对象列表,这将起作用:

missing_age_objects = [l[idx] for idx in missing_age_indexes]

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

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