質問編集履歴

4

やってみたこと、微修正

2023/04/10 07:58

投稿

syayo
syayo

スコア22

test CHANGED
File without changes
test CHANGED
@@ -60,9 +60,9 @@
60
60
  }
61
61
  ```
62
62
 
63
- #追記:やってみたこと
64
63
 
64
+
65
- ###やってみたこと
65
+ ### やってみたこと
66
66
  以下のコードを実行してみましたが、その他の最終階層に所有者の値が入りません。
67
67
 
68
68
  ```Python
@@ -99,7 +99,7 @@
99
99
  with open('example.json', 'w') as f:
100
100
  json.dump(data, f, ensure_ascii=False, indent=4)
101
101
  ```
102
- ###出力結果(example.json)
102
+ ### 出力結果(example.json)
103
103
  ```Json
104
104
  {
105
105
  "果物": {

3

やってみたことを追記しました。

2023/04/10 07:57

投稿

syayo
syayo

スコア22

test CHANGED
File without changes
test CHANGED
@@ -60,3 +60,72 @@
60
60
  }
61
61
  ```
62
62
 
63
+ #追記:やってみたこと
64
+
65
+ ###やってみたこと
66
+ 以下のコードを実行してみましたが、その他の最終階層に所有者の値が入りません。
67
+
68
+ ```Python
69
+ import pandas as pd
70
+ import json
71
+
72
+ # Read the Excel file
73
+ df = pd.read_excel('sample.xlsx')
74
+
75
+ # Create a dictionary to hold the data
76
+ data = {}
77
+
78
+ # Iterate over the rows of the DataFrame
79
+ for index, row in df.iterrows():
80
+ # Create a reference to the dictionary for the current row
81
+ current_dict = data
82
+
83
+ # Iterate over the values in the row
84
+ for i, value in enumerate(row[:-1]):
85
+ # If the value is not empty, use it as a key in the dictionary
86
+ if not pd.isna(value):
87
+ # If the key does not exist in the dictionary, create it
88
+ if value not in current_dict:
89
+ # If this is the last column, the value is the owner
90
+ if i == len(row)-2:
91
+ current_dict[value] = row[-1]
92
+ # Otherwise, create a new dictionary
93
+ else:
94
+ current_dict[value] = {}
95
+ # Update the reference to the dictionary for the current row
96
+ current_dict = current_dict[value]
97
+
98
+ # Convert the dictionary to JSON and write it to a file
99
+ with open('example.json', 'w') as f:
100
+ json.dump(data, f, ensure_ascii=False, indent=4)
101
+ ```
102
+ ###出力結果(example.json)
103
+ ```Json
104
+ {
105
+ "果物": {
106
+ "赤": {
107
+ "りんご": "A",
108
+ "いちご": "A"
109
+ },
110
+ "黄色": {
111
+ "ばなな": "B",
112
+ "れもん": "C"
113
+ }
114
+ },
115
+ "野菜": {
116
+ "赤": {
117
+ "にんじん": "C",
118
+ "とまと": "A"
119
+ },
120
+ "緑": {
121
+ "きゅうり": "B",
122
+ "せろり": "A"
123
+ }
124
+ },
125
+ "その他": {
126
+ "赤": {},
127
+ "黄色": {}
128
+ }
129
+ }
130
+ ```
131
+

2

Jsonを修正

2023/04/10 06:21

投稿

syayo
syayo

スコア22

test CHANGED
File without changes
test CHANGED
@@ -25,7 +25,7 @@
25
25
 
26
26
  ```JSON
27
27
  {
28
- "階層1": {
28
+ "果物": {
29
29
  "赤": [
30
30
  {
31
31
  "りんご": "A",
@@ -39,7 +39,7 @@
39
39
  }
40
40
  ]
41
41
  },
42
- "階層2": {
42
+ "野菜": {
43
43
  "赤": [
44
44
  {
45
45
  "にんじん": "C",

1

ルールを追記しました。

2023/04/10 06:06

投稿

syayo
syayo

スコア22

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,10 @@
1
1
  ### やりたいこと
2
2
  ExcelやCSVの表形式のデータをJsonファイルに出力するにはどのようにしたらいいでしょうか?
3
3
  Pandasを使ってやってみたりしましたが、そもそもどこからやっていいかわからず質問させていただきました。
4
+
5
+ ### 追記
6
+ 階層1の中に階層2、階層3を入れていきたいのと、
7
+ 階層の深さによらず、最下層と所有者を辞書形式にしたいです。
4
8
 
5
9
  ### 操作前データ
6
10
  |階層1|階層2|階層3|所有者|