0

我有一个带有 JSON 格式列的 Pandas 数据框,在将整个 DF 导出为 JSON 时,JSON-Coloumn 将像普通字符串一样格式化:

foo = pd.DataFrame({'a' : [1,2,3,4], 'b': ['a','b', 'c','d']})

foo['json'] = foo.apply(lambda x: x[['b']].to_json(), axis=1)

OUTPUT:
   a  b       json
0  1  a  {"b":"a"}
1  2  b  {"b":"b"}
2  3  c  {"b":"c"}
3  4  d  {"b":"d"}
foo.to_json()

OUTPUT:

{"a":{"0":1,"1":2,"2":3,"3":4},"b":{"0":"a","1":"b","2":"c","3":"d"},"json":{"0":"{\"b\":\"a\"}","1":"{\"b\":\"b\"}","2":"{\"b\":\"c\"}","3":"{\"b\":\"d\"}"}}

如何在没有正斜杠的情况下导出它?

4

1 回答 1

1

最简单的解决方案是 apply to_dict(),这是最原生的 pandas 方法,尽管我不确定它是否是最高效的方法。

foo.to_json()


OUTPUT:

'{"a":{"0":1,"1":2,"2":3,"3":4},"b":{"0":"a","1":"b","2":"c","3":"d"},"json":{"0":{"b":"a"},"1":{"b":"b"},"2":{"b":"c"},"3":{"b":"d"}}}'
于 2020-01-10T15:10:51.270 回答