質問編集履歴

3

文章の加筆修正

2022/08/21 11:17

投稿

Tanutanu
Tanutanu

スコア1

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  https://note.com/maytakesao/n/n126165676d30
4
4
 
5
- このうち、《変更前》のコードにおいて、
5
+ このうち、《変更前コードにおいて、
6
+
7
+ 《変更前エラー》
6
8
  ```Python
7
9
  ---------------------------------------------------------------------------
8
10
  TypeError Traceback (most recent call last)
@@ -22,8 +24,10 @@
22
24
 
23
25
 
24
26
  そこで、astypeを用いて、文字列(str)から整数(int)に変換を試み、
25
- 《変更後》のようなコードにしたところ、'面積(㎡)'に対してKeyerrorが出てしまい、
27
+ 《変更後コード》のように記載したところ、'面積(㎡)'に対してKeyerrorが出てしまい、
26
28
  実行できませんでした。解決方法を教えて頂けるとありがたいです。
29
+
30
+ 《変更後エラー》
27
31
  ```Python
28
32
  ---------------------------------------------------------------------------
29
33
  KeyError Traceback (most recent call last)
@@ -46,7 +50,7 @@
46
50
  ```
47
51
 
48
52
 
49
- 《変更前》
53
+ 《変更前コード
50
54
  ```Python
51
55
  #中古マンション等のみ抽出
52
56
  df_data_normal = df_data_normal[df_data_normal['種類'] == '中古マンション等']
@@ -56,7 +60,7 @@
56
60
  df_data_normal = df_data_normal.loc[df_data_normal['建築年(西暦)'] >= 1950]
57
61
  ```
58
62
 
59
- 《変更後》
63
+ 《変更後コード
60
64
  ```Python
61
65
  #中古マンション等のみ抽出
62
66
  df_data_normal = df_data_normal[df_data_normal['種類'] == '中古マンション等']

2

エラー内容をすべて貼り付け

2022/08/21 11:14

投稿

Tanutanu
Tanutanu

スコア1

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,18 @@
4
4
 
5
5
  このうち、《変更前》のコードにおいて、
6
6
  ```Python
7
+ ---------------------------------------------------------------------------
8
+ TypeError Traceback (most recent call last)
9
+ <ipython-input-29-63695056f7a7> in <module>
10
+ 2 df_data_normal = df_data_normal[df_data_normal['種類'] == '中古マンション等']
11
+ 3 #面積(㎡)⇒120以内を使う, 最寄駅:距離(分)⇒30以内を使う,建築年(西暦) ⇒1950年以上を使う
12
+ ----> 4 df_data_normal = df_data_normal.loc[df_data_normal['面積(㎡)'] <=120]
13
+ 5 df_data_normal = df_data_normal.loc[df_data_normal['最寄駅:距離(分)'] <=30]
14
+ 6 df_data_normal = df_data_normal.loc[df_data_normal['建築年(西暦)'] >= 1950]
15
+
16
+ 5 frames
17
+ /usr/local/lib/python3.7/dist-packages/pandas/_libs/ops.pyx in pandas._libs.ops.scalar_compare()
18
+
7
19
  TypeError: '<=' not supported between instances of 'str' and 'int'
8
20
  ```
9
21
  というエラーが出ます。
@@ -12,6 +24,27 @@
12
24
  そこで、astypeを用いて、文字列(str)から整数(int)に変換を試み、
13
25
  《変更後》のようなコードにしたところ、'面積(㎡)'に対してKeyerrorが出てしまい、
14
26
  実行できませんでした。解決方法を教えて頂けるとありがたいです。
27
+ ```Python
28
+ ---------------------------------------------------------------------------
29
+ KeyError Traceback (most recent call last)
30
+ <ipython-input-28-add3df40f807> in <module>
31
+ 2 df_data_normal = df_data_normal[df_data_normal['種類'] == '中古マンション等']
32
+ 3 #面積(㎡)⇒120以内を使う, 最寄駅:距離(分)⇒30以内を使う,建築年(西暦) ⇒1950年以上を使う
33
+ ----> 4 df['面積(㎡)'] = df['面積(㎡)'].astype(int)
34
+ 5 df['最寄駅:距離(分)'] = df['最寄駅:距離(分)'].astype(int)
35
+ 6 df['建築年(西暦)'] = df['建築年(西暦)'].astype(int)
36
+
37
+ 2 frames
38
+ /usr/local/lib/python3.7/dist-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)
39
+ 386 except ValueError as err:
40
+ 387 raise KeyError(key) from err
41
+ --> 388 raise KeyError(key)
42
+ 389 return super().get_loc(key, method=method, tolerance=tolerance)
43
+ 390
44
+
45
+ KeyError: '面積(㎡)'
46
+ ```
47
+
15
48
 
16
49
  《変更前》
17
50
  ```Python

1

コードの挿入方法の変更、およびエラー内容の具体化

2022/08/21 11:10

投稿

Tanutanu
Tanutanu

スコア1

test CHANGED
File without changes
test CHANGED
@@ -2,20 +2,39 @@
2
2
 
3
3
  https://note.com/maytakesao/n/n126165676d30
4
4
 
5
+ このうち、《変更前》のコードにおいて、
6
+ ```Python
7
+ TypeError: '<=' not supported between instances of 'str' and 'int'
8
+ ```
9
+ というエラーが出ます。
5
10
 
6
- このうち、以下のコードにおいてエラーが出ます。
7
- astypeを用いて、文字列(str)から整数(int)に変換を試みましたが、
8
- うまくいかず、解決方法を教えて頂けるとありがたいです。
9
11
 
12
+ そこで、astypeを用いて、文字列(str)から整数(int)に変換を試み、
10
- ------------------------------------------------------------------------------------------
13
+ 《変更後》のようなコードにしたところ、'面積(㎡)'に対してKeyerrorが出てしまい、
14
+ 実行できませんでした。解決方法を教えて頂けるとありがたいです。
15
+
16
+ 《変更前》
17
+ ```Python
11
18
  #中古マンション等のみ抽出
12
19
  df_data_normal = df_data_normal[df_data_normal['種類'] == '中古マンション等']
13
-
14
20
  #面積(㎡)⇒120以内を使う, 最寄駅:距離(分)⇒30以内を使う,建築年(西暦) ⇒1950年以上を使う
15
21
  df_data_normal = df_data_normal.loc[df_data_normal['面積(㎡)'] <=120]
16
22
  df_data_normal = df_data_normal.loc[df_data_normal['最寄駅:距離(分)'] <=30]
17
23
  df_data_normal = df_data_normal.loc[df_data_normal['建築年(西暦)'] >= 1950]
24
+ ```
25
+
26
+ 《変更後》
27
+ ```Python
28
+ #中古マンション等のみ抽出
18
- ------------------------------------------------------------------------------------------
29
+ df_data_normal = df_data_normal[df_data_normal['種類'] == '中古マンション等']
30
+ #面積(㎡)⇒120以内を使う, 最寄駅:距離(分)⇒30以内を使う,建築年(西暦) ⇒1950年以上を使う
31
+ df['面積(㎡)'] = df['面積(㎡)'].astype(int)
32
+ df['最寄駅:距離(分)'] = df['最寄駅:距離(分)'].astype(int)
33
+ df['建築年(西暦)'] = df['建築年(西暦)'].astype(int)
34
+ df_data_normal = df_data_normal.loc[df_data_normal[df['面積(㎡)'] <=120]]
35
+ df_data_normal = df_data_normal.loc[df_data_normal[df['最寄駅:距離(分)'] <=30]]
36
+ df_data_normal = df_data_normal.loc[df_data_normal[df['建築年(西暦)'] >= 1950]]
37
+ ```
19
38
 
20
39
  よろしくおねがいします。
21
40