teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

defでの関数定義を追記

2017/11/09 23:42

投稿

y__sama
y__sama

スコア83

answer CHANGED
@@ -20,4 +20,13 @@
20
20
  # > 1 ProductB False True False key2
21
21
  # > 2 ProductC True False True key1,key3
22
22
 
23
+ ```
24
+
25
+ 無理にlambdaを使わなくても普通の関数でもOKです。
26
+ ```python
27
+ def func(series):
28
+ columnNames=series.index[series==True].tolist()
29
+ return ",".join(columnNames)
30
+
31
+ df['keys'] = df.apply(func, axis = 1)
23
32
  ```

1

Seriesが大文字はじまりになっており、クラスと誤解しやすいので小文字に修正

2017/11/09 23:42

投稿

y__sama
y__sama

スコア83

answer CHANGED
@@ -4,7 +4,7 @@
4
4
  import pandas as pd
5
5
 
6
6
  # 行ごとのデータを受け取って、Trueの列だけのindexをリストに変換してjoinするlambda関数を定義
7
- f = lambda Series: ",".join(Series.index[Series==True].tolist())
7
+ f = lambda series: ",".join(series.index[series==True].tolist())
8
8
 
9
9
  # 入出力例
10
10
  f(df.iloc[2])