gpt4 book ai didi

python - 如何避免在 Pandas 数据框中自动更改数据类型并在python中转换为CSV?

转载 作者:行者123 更新时间:2023-12-04 10:49:26 25 4
gpt4 key购买 nike

我正在尝试使用 python 中的 Pandas 将 Json 文件转换为 csv

json文件数据:

[{
"source": "https://www.na-kd.com/en/sweaters/cardigans/button-up-ribbed-cropped-cardigan-pink",
"class_ids": "3_33",
"id_matrix": "0_0_0_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_1_0_0_0_0_0_0_0_0_0_0_0_0",
"tags": "cardigan_neckline",
"front": "https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_01g.jpg",
"back": "https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_02a.jpg",
"left": "https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_03b.jpg",
"right": "https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_04c.jpg",
"zoomedin": "https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_05g.jpg",
"otherurl": "https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_05g.jpg"
}, {...}, {...}]

我要转换为 CSV 文件的代码..(但 Pandas 会自动更改“id_matrix”和“class_ids”的数据类型,我想要这些列字符串..
raw_data=pd.read_json('/home/mobin/PycharmProjects/na-kd/Jsons/mapped_improvedcheck.json')
raw_data.to_csv("csv_file/samplecheck.csv")

result = raw_data.dtypes
print(result)
print(raw_data['id_matrix'][:10])

此代码的输出:
source        object
class_ids int64
id_matrix float64
tags object
front object
back object
left object
right object
zoomedin object
otherurl object
dtype: object
0 1.000000e+43
1 1.000000e+43
2 1.000000e+43
3 1.000000e+43
4 1.000000e+43
5 1.000000e+43
6 1.000000e+43
7 1.000000e+43
8 1.000000e+43
9 1.000000e+43

最佳答案

您可以使用 astype pandas.DataFrame 的属性:

import pandas as pd

raw_data = pd.read_json('/home/mobin/PycharmProjects/na-kd/Jsons/mapped_improvedcheck.json')
raw_data2 = raw_data.astype('object')
raw_data2.to_csv('csv_file/samplecheck.csv')

result = raw_data2.dtypes
print(result)
print(raw_data2['id_matrix'][:10])

更新:我在 samplecheck.csv 里面得到了什么文件是:
,back,class_ids,front,id_matrix,left,otherurl,right,source,tags,zoomedin
0,https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_02a.jpg,333,https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_01g.jpg,1e+42,https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_03b.jpg,https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_05g.jpg,https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_04c.jpg,https://www.na-kd.com/en/sweaters/cardigans/button-up-ribbed-cropped-cardigan-pink,cardigan_neckline,https://www.na-kd.com/globalassets/nakd_button_up_ribbed_cropped_cardigan_1018-004495-0211_05g.jpg

关于python - 如何避免在 Pandas 数据框中自动更改数据类型并在python中转换为CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com