>>> print name_frame
...
name name1 name2 name3 name4
Micro inc. NaN Jim D Susan A NaN NaN
Vitacore Billy B NaN Sally Q Mark G NaN
>>> payload = name_frame.apply(lambda x: [x.dropna()], axis=1).to_json(force_ascii=False)
...
>>> print payload
...
{
"Micro inc.":[{"name1":"Jim D","name2":"Susan A"}],
"Vitacore":[{"name":"Billy B","name2":"Sally Q","name3":"Mark G"}],
}
finalJSON = {
"company":{
"name": "Micro inc.",
"founders": {
"name": "Jim D",
"name": "Susan A",
}
}
"company":{
"name": "Vitacore",
"founders": {
"name": "Billy B",
"name": "Sall Q",
"name":"Mark G",
}
有没有人知道我如何完成这些任务的工具,库或一般建议?我需要将每个公司对象作为POST请求发送到API,它需要这种格式。从那里我需要将结果附加到pandas DataFrame。 我认为应该涉及循环JSON数据,向每家公司提交API,获取结果并将其添加到字典中,或者如果可能的话直接添加到Pandas DataFrame
payload= '''a single company from finalJSON'''
#p is a POST Request
p = requests.post((url + '/r'), json=payload, headers=headers)
p.text #<---- gotta go to a Pandas DataFrame
提前感谢您提供任何帮助或建议
答案 0 :(得分:1)
finalJSON = []
for company, names in df.iterrows():
names = ['"{0}"'.format(name) for name in names.dropna().tolist()]
names_json_str = ('"name": ' if names else '') + ', "name": '.join(names)
finalJSON.append('"company": {"name": "' + company + '", "founders": {' + names_json_str + '}')
finalJSON = ', '.join(finalJSON)
>>> finalJSON
'"company": {"name": "Micro inc.", "founders": {"name": "Jim D", "name": "Susan A"},
"company": {"name": "Vitacore", "founders": {"name": "Billy B", "name": "Sally Q", "name": "Mark G"}'