简体   繁体   English

使用 Python 将 Excel 文件转换为符合特定格式的 Json

[英]Convert Excel file to Json respecting a specific format using Python

I would like to convert the following sheet of an excel file containing coordinates into a json file that looks exactly like the one below.我想将以下包含坐标的 excel 文件转换为一个 json 文件,该文件与下面的文件完全相同。 I need it to be that way in order to run a clustering algorithm.我需要这样才能运行聚类算法。

Thanks谢谢

在此处输入图片说明

 { "X" : [[1.32, 2.23], [2.01, 2.223], [4.196, 4.04], [4.09, 3.96], [2.01, 3.01], [8.01, 7.01], [8.01, 8.01], [1.01, 8.01], [1.01, 1.10], [0.10, 7.81], [0.10, 7.91], [0.1, 7.91], [0.01, 7.8], [0.1, 7.8], [6.875, 1.43], [6.99, 1.54], [6.71, 1.37], [7.98, 1.1], [7.33, 1.53], [6.43, 1.3], [6.99, 1.3], [4.11, 4.11]] }

Can you try this solution and adapt it to your dataframe:您可以尝试使用此解决方案并使其适应您的数据框吗:

Code:代码:

import pandas as pd

# Creating Dataframe
df = pd.DataFrame([[1, 2],
                   [3, 4],
                   [5, 6],
                   [7, 8]
                   ],
                  columns=['x', 'y'])

# Convert DataFrame to JSON
data = df.to_json(orient='values')
output = '{ "X" :'+data+'}'
print(output)

Output:输出:

{ "X" :[[1,2],[3,4],[5,6],[7,8]]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM