首页 > 解决方案 > 从复杂的 Csv/DataFrame 加载 Json,为 MongoDB 保留 dtypes

问题描述

我正在尝试从一些不同的 csv/excel 文件中为可查询的 MongoDB 在 json 树上构建。数据通常不完整,并由主题 ID 链接。

下面的示例数据:

subid,firstvisit,name,contact,dob,gender,visitdate1,age,visitcategory,samplenumber,label_on_sample,completed_by
    1,12/31/11,Bob,,12/31/00,Male,,,,,,
    1,,,,,,12/31/15,17,Baseline Visit,,,
    1,,,,,,12/31/16,18,Follow Up Visit,,,
    1,,,,,,12/31/17,18,Follow Up Visit,,,
    1,,,,12/31/00,Male,,17,,XXX123,1,Sally
    2,1/1/12,,,1/1/01,Female,,,,,,
    2,,,,,,1/1/11,10,Baseline Visit,,,
    2,,,,,,1/1/12,11,Follow Up Visit,,,
    2,,,,,,1/1/13,12,Follow Up Visit,,,
    2,,,,,,1/1/14,13,Follow Up Visit,,,
    2,,,,,,1/1/15,14,Follow Up Visit,,,
    2,,,,1/1/01,Female,,15,,YYY456,2,
    2,,,,1/1/01,Female,,15,,ZZZ789,2,Sally'

我希望输出看起来像这样:

[
    {
        "subject_id": "1",
        "name": "Bob",
        "dob": "12/31/00",
        "gender": "Male",
        "visits": {
            "12/31/15": {
                "age": "17",
                "visit_category": "Baseline Visit"
            },
            "12/31/16": {
                "age": "18",
                "visit_category": "Follow Up Visit"
            },
            "12/31/17": {
                "age": "18",
                "visit_category": "Follow Up Visit"
            }
        },
        "samples": {
            "XXX123": {
                "completed_by": "Sally",
                "label_on_sample": "1"
            }
        }
    },
    {
        "subject_id": "2",
        "name": null,
        "dob": "1/1/01",
        "gender": "Female",
        "visits": {
            "1/1/11": {
                "age": "10",
                "visit_category": "Baseline Visit"
            },
            "1/1/12": {
                "age": "11",
                "visit_category": "Follow Up Visit"
            },
            "1/1/13": {
                "age": "12",
                "visit_category": "Follow Up Visit"
            },
            "1/1/14": {
                "age": "13",
                "visit_category": "Follow Up Visit"
            },
            "1/1/15": {
                "age": "14",
                "visit_category": "Follow Up Visit"
            }
        },
        "samples": {
            "YYY456": {
                "completed_by": null,
                "label_on_sample": "2"
            },
            "ZZZ789": {
                "completed_by": "Sally",
                "label_on_sample": "2"
            }
        }
    }
]

我有一个程序将所有这些都放入正确的结构中,但不幸的是,因为它使用 csv 的 DictReader,似乎所有变量都作为字符串输入,因此很难以有意义的方式进行查询。这段代码如下:

def solution(csv_filename):
    by_subject_id = defaultdict(lambda: {
        'name': None,
        'dob': None,
        'gender': None,
        'visits': {},
        'samples': {}
    })

    with open(csv_filename) as f:
        dict_reader = DictReader(f)
        for row in dict_reader:
            non_empty = {k: v for k, v in row.items() if v}
            subject_id = non_empty['subid']  # must have to group by
            first_visit = non_empty.get('firstvisit')  # optional
            sample = non_empty.get('samplenumber')  # optional
            visit = non_empty.get('visitdate1')  # optional

            if first_visit:
                by_subject_id[subject_id].update({
                    'name': non_empty.get('name'),
                    'dob': non_empty.get('dob'),
                    'gender': non_empty.get('gender')
                })
            elif visit:
                by_subject_id[subject_id]['visits'][visit] = {
                    'age': non_empty.get('age'),
                    'visit_category': non_empty.get('visitcategory')
                }
            elif sample:
                by_subject_id[subject_id]['samples'][sample] = {
                    'completed_by': non_empty.get('completed_by'),
                    'label_on_sample': non_empty.get('label_on_sample')
                }
    return [{'subject_id': k, **v} for k, v in by_subject_id.items()]

解决此问题的最佳方法是什么?我可以将其转换为适用于数据框并希望保留数据类型吗?

非常感谢您的任何建议。Mongo 的新手,只是想得到一些有用的东西。

标签: pythonjsonmongodbpandas

解决方案


这不是最好的解决方案,但使用 pandas 可能有助于保持值的类型,我没有看代码的效率,只是阅读 csv 文件的部分,但你可以这样做:

import pandas as pd
def solution(csv_filename):
    by_subject_id = defaultdict(lambda: {
        .
        .
    })

    df = pd.read_csv(csv_filename).fillna('')
    for row in df .iterrows():
        non_empty = {k: v for k, v in row[1].iteritems() if  v != ''}
        subject_id = non_empty['subid']  # must have to group by
        .
        .
        .

我尽量保持几行改变,其他一切都是一样的。最终,如果您可以直接将清理后的 DF 作为参数而不是读取 csv 文件,那就更好了。否则,您可以添加dtype=read_csv() 例如:

df = pd.read_csv(csv_filename,dtype={'subid':int, 'age':int}).fillna('')

添加您想要的任何类型。

希望对你有帮助


推荐阅读