質問編集履歴

1

追加質問

2022/11/28 10:59

投稿

tom57
tom57

スコア29

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,18 @@
14
14
  df['col3'] = np.where(df.index == df.index[-1], '1','0')
15
15
 
16
16
  ```
17
+ <追加>
18
+ 下記のように条件を追加したい場合を試してはみたのですが、うまくいかず…
19
+ 1度に質問すべきだったのですが、申し訳ありません。
20
+
21
+ ```ここに言語を入力
22
+ import pandas as pd
23
+
24
+ df = pd.DataFrame({'group':['A','A','B','B'],'col1': [0, 3, 2, 3], 'col2': [4, 0, 2, 1]})
25
+ df['col3'] =""
26
+ df.loc[(df.groupby('group').tail(1).index) & (df['group']=='A'), 'col3'] = "1"
27
+ ```
28
+
29
+ IndexError: Boolean index has wrong length: 2 instead of 4
17
30
 
18
31
 
19
-