I have a pandas dataframe like below
data = pd.DataFrame([
{"name" : "c","email" : "[email protected]","ml_pred":"valid","hum_pred":"null","score":0.83},
{"name" : "d","email" : "[email protected]","ml_pred":"invalid","hum_pred":"null","score":0.12}
])
How can this dataframe be converted to the following format
[
({"email": {"$eq": "[email protected]"}},{"$set": {"ml_pred": "valid", "score": 0.83}}),
({"email": {"$eq": "[email protected]"}},{"$set": {"ml_pred": "invalid", "score": 0.12}})
]
CodePudding user response:
You could use DataFrame.apply
and construct a tuple for each row:
out = (data.apply(lambda x: ({"email": {"$eq": x["email"]}},
{"$set": {"ml_pred": x["ml_pred"], "score": x["score"]}}),
axis=1).tolist())
Output:
[({'email': {'$eq': '[email protected]'}}, {'$set': {'ml_pred': 'valid', 'score': 0.83}}),
({'email': {'$eq': '[email protected]'}}, {'$set': {'ml_pred': 'invalid', 'score': 0.12}})]