如何在pandas数据框中,如果字段为null或NaN,则不将其插入到Mongo集合中?
如何在pandas数据框中,如果字段为null或NaN,则不将其插入到Mongo集合中?
我从SQL获取数据,进行一些操作,然后将其转换为Pandas DataFrame,最后将其插入到Mongo集合中。但是在Mongo中,显示了一些具有空值的字段,这是我不想要的。
下面是DataFrame的内容:
person = [ { "name" : "Tom", "location" : "Pune", "zone" : "Red", "profession" :"IT" }, { "name" : "Jerry", "location" : "Mumbai", "profession":"" } ] df = pd.DataFrame(person)
这是我将DataFrame插入Mongo的方式:
def bulkInsertData(collectionName, df): try: records = json.loads(df.to_json()).values() dbInst[collectionName].insert_many(records, ordered=False, bypass_document_validation=True) except BulkWriteError as bwe: print(bwe.details) except UnicodeEncodeError as bwe: print(bwe.details)
但是所有字段都显示了。如你所见,在Jerry的情况下,Mongo中不应插入'zone'和'profession'字段。请给出一些建议。