給定的框架是這樣的:從熊貓數據幀過濾掉唯一的真值,返回(行,列)的元組
a b c
1 True False False
2 True True False
3 False True True
我希望得到像這樣的列表:
[(1,a), (2,a), (2,b), (3,b), (3,c)]
那過濾掉所有真值並檢索元組的值(rowName,colName)
給定的框架是這樣的:從熊貓數據幀過濾掉唯一的真值,返回(行,列)的元組
a b c
1 True False False
2 True True False
3 False True True
我希望得到像這樣的列表:
[(1,a), (2,a), (2,b), (3,b), (3,c)]
那過濾掉所有真值並檢索元組的值(rowName,colName)
另一種方法是使用stack
:
>>> s = df.stack()
>>> s[s].index.tolist()
[(0L, 'a'), (1L, 'a'), (1L, 'b'), (2L, 'b'), (2L, 'c')]
這工作,因爲這裏stack
返回扁平版本:
>>> df.stack()
0 a True
b False
c False
1 a True
b True
c False
2 a False
b True
c True
dtype: object
太棒了! – unutbu
總是看起來如此簡單:) – user3139545
甚至可以在一行'df [df] .stack()。index.tolist()'中做到這一點。儘管'df [df]'比's'慢得多。 –
In [27]: df
Out[27]:
a b c
1 True False False
2 True True False
3 False True True
[3 rows x 3 columns]
您可以找到與True
個S使用np.where
:
In [28]: np.where(df)
Out[28]: (array([0, 1, 1, 2, 2]), array([0, 0, 1, 1, 2]))
In [29]: x, y = np.where(df)
索引和列ndarrays,您可以選擇使用NumPy的整數索引標籤:
In [30]: df.index[y]
Out[30]: Int64Index([1, 1, 2, 2, 3], dtype='int64')
In [31]: df.columns[x]
Out[31]: Index([u'a', u'b', u'b', u'c', u'c'], dtype='object')
與zip
放在一起:
In [32]: zip(df.index[y], df.columns[x])
Out[32]: [(1, 'a'), (1, 'b'), (2, 'b'), (2, 'c'), (3, 'c')]
確定。你有什麼嘗試? – sshashank124