質問編集履歴

1

エラーメッセージを原文のままコピー/ソースコードを貼りなおしました

2021/06/30 09:08

投稿

syoooo
syoooo

スコア1

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- ### 前提・実現したいこと
1
+ ```### 前提・実現したいこと
2
2
 
3
3
  pythonのデータフレームを引数として関数利用についてご質問です。
4
4
 
@@ -18,15 +18,85 @@
18
18
 
19
19
  エラーメッセージ
20
20
 
21
+ raceback (most recent call last):
22
+
23
+ File "C:\Users\yasuda3809\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\indexes\base.py", line 3081, in get_loc
24
+
25
+ return self._engine.get_loc(casted_key)
26
+
27
+ File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
28
+
29
+ File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
30
+
31
+ File "pandas\_libs\hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
32
+
33
+ File "pandas\_libs\hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
34
+
35
+ KeyError: ('x', 'y')
36
+
37
+
38
+
39
+ The above exception was the direct cause of the following exception:
40
+
41
+
42
+
21
43
  Traceback (most recent call last):
22
44
 
23
45
  File "test.py", line 13, in <module>
24
46
 
25
- df["new_xy"]=df["x","y"].apply(add_two)
47
+ df["new_xy"]=df["x","y"].apply(add_two)
48
+
49
+ File "C:\Users\yasuda3809\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\frame.py", line 3024, in __getitem__
50
+
51
+ indexer = self.columns.get_loc(key)
52
+
53
+ File "C:\Users\yasuda3809\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\indexes\base.py", line 3083, in get_loc
54
+
55
+ raise KeyError(key) from err
56
+
57
+ KeyError: ('x', 'y')
58
+
59
+
60
+
61
+ ```ここに言語```
62
+
63
+ コード
64
+
65
+ ```を入力
66
+
67
+ import pandas as pd
68
+
69
+ df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=['x','y','z'])
70
+
71
+
72
+
73
+ def add_one(x):
74
+
75
+ return x+1
76
+
77
+
78
+
79
+ def add_two(x,y):
80
+
81
+ return x+y
82
+
83
+
84
+
85
+ df["new_x"]=df["x"].apply(add_one)
26
86
 
27
87
 
28
88
 
29
89
 
90
+
91
+ df["new_xy"]=df["x","y"].apply(add_two)
92
+
93
+
94
+
95
+ print(df)
96
+
97
+
98
+
99
+ ```
30
100
 
31
101
  ### 該当のソースコード
32
102