回答編集履歴
1
コード修正
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
df = pd.DataFrame({'num1':[1,11,50], 'num2':[3,5,7]})
|
9
|
+
df = pd.DataFrame({'name1':list('aaabbb'), 'num1':[1,11,50]*2, 'num2':[3,5,7]*2})
|
10
10
|
|
11
11
|
A = 5
|
12
12
|
|
@@ -28,18 +28,28 @@
|
|
28
28
|
|
29
29
|
|
30
30
|
|
31
|
-
df['num3'] = df.apply(func, axis=1)
|
31
|
+
df['num3'] = df[df['name1']=='a'].apply(func, axis=1)
|
32
32
|
|
33
|
-
|
33
|
+
#df['num3'] = 0 # NaNがいやなら必要に応じてデフォルト値を設定
|
34
34
|
|
35
35
|
print(df)
|
36
36
|
|
37
|
-
|
37
|
+
"""
|
38
38
|
|
39
|
-
|
39
|
+
name1 num1 num2 num3
|
40
40
|
|
41
|
-
|
41
|
+
0 a 1 3 5
|
42
42
|
|
43
|
+
1 a 11 5 250
|
44
|
+
|
43
|
-
|
45
|
+
2 a 50 7 error
|
46
|
+
|
47
|
+
3 b 1 3 NaN
|
48
|
+
|
49
|
+
4 b 11 5 NaN
|
50
|
+
|
51
|
+
5 b 50 7 NaN
|
52
|
+
|
53
|
+
"""
|
44
54
|
|
45
55
|
```
|