こんな感じで。列ごとにユニークな要素の個数を数え、1でない列だけ残します。
python
1import io
2import pandas as pd
3
4txt = """
5a,b,c,d,e,f
60,0,1,5,5,5
74,5,3,5,4,5
80,7,5,5,4,5
94,5,6,5,7,5
104,7,8,5,4,5
11"""
12
13df = pd.read_csv(io.StringIO(txt), header=0)
14print(df)
15""" =>
16 a b c d e f
170 0 0 1 5 5 5
181 4 5 3 5 4 5
192 0 7 5 5 4 5
203 4 5 6 5 7 5
214 4 7 8 5 4 5
22"""
23
24print(df.nunique())
25""" =>
26a 2
27b 3
28c 5
29d 1
30e 3
31f 1
32dtype: int64
33"""
34
35print(df.loc[:,~(df.nunique()==1)])
36""" =>
37 a b c e
380 0 0 1 5
391 4 5 3 4
402 0 7 5 4
413 4 5 6 7
424 4 7 8 4
43"""
参考:
pandasでユニークな要素の個数、頻度(出現回数)をカウント | note.nkmk.me
追記
「すべて任意の数(たとえば5)の列を除去したい」ということだったので、
python
1import io
2import pandas as pd
3
4txt = """
5a,b,c,d,e,f
60,0,1,5,5,5
74,5,3,5,4,5
80,7,5,5,4,5
94,5,6,5,7,5
104,7,8,5,4,5
11"""
12
13df = pd.read_csv(io.StringIO(txt), header=0)
14print(df)
15""" =>
16 a b c d e f
170 0 0 1 5 5 5
181 4 5 3 5 4 5
192 0 7 5 5 4 5
203 4 5 6 5 7 5
214 4 7 8 5 4 5
22"""
23
24print(df==5)
25""" =>
26 a b c d e f
270 False False False True True True
281 False True False True False True
292 False False True True False True
303 False True False True False True
314 False False False True False True
32"""
33
34print(~(df == 5).all(axis=0))
35""" =>
36a True
37b True
38c True
39d False
40e True
41f False
42dtype: bool
43"""
44
45print(df.loc[:, ~(df == 5).all(axis=0)])
46""" =>
47 a b c e
480 0 0 1 5
491 4 5 3 4
502 0 7 5 4
513 4 5 6 7
524 4 7 8 4
53"""
df全体を5と比較してbooleanにし、~(df == 5).all(axis=0)
で残す列を選択します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/02 13:58
2018/08/02 14:12
2018/08/02 14:18