[英]python pandas - how to create for each row a list of column names with a condition?
[英]Get the column names for each index(row) such that column value is imposed upon some condition in pandas
我有以下內容:
>>> import pandas as pd
>>> x = pd.DataFrame({'a':[1,3,5], 'b':[4,0,6]})
>>> x
a b
0 1 4
1 3 0
2 5 6
>>> required = {0:['b'],1:['a'],2:['a','b']} ---> how to get it from x??
#keys -> index of x
#values -> list of col names such that value is >2
我們如何才能有效地做到這一點?
這是使用apply
和to_dict
方法的to_dict
。
In [162]: (x > 2).apply(lambda y: x.columns[y.tolist()].tolist(), axis=1).to_dict()
Out[162]: {0: ['b'], 1: ['a'], 2: ['a', 'b']}
細節
In [173]: (x > 2)
Out[173]:
a b
0 False True
1 True False
2 True True
In [174]: (x > 2).apply(lambda y: [y.tolist()], axis=1)
Out[174]:
0 [[False, True]]
1 [[True, False]]
2 [[True, True]]
dtype: object
In [175]: (x > 2).apply(lambda y: x.columns[y.tolist()].tolist(), axis=1)
Out[175]:
0 [b]
1 [a]
2 [a, b]
dtype: object
這是另一條線。
In [205]: {i: x.columns[y.tolist()].tolist() for i, y in (x > 2).iterrows()}
Out[205]: {0: ['b'], 1: ['a'], 2: ['a', 'b']}
要么
In [122]: {i: y[y].index.tolist() for i, y in (x > 2).iterrows()}
Out[122]: {0: ['b'], 1: ['a'], 2: ['a', 'b']}
這是兩個有效的想法:
pd.DataFrame(x.columns.where(x > 2, ''))
Out:
0
0 (, b)
1 (a, )
2 (a, b)
np.where(x > 2, x.columns, '').T
Out:
array([['', 'a', 'a'],
['b', '', 'b']], dtype=object)
不了解效率,但可以:
df = pd.DataFrame({'a':[1,3,5], 'b':[4,0,6]})
a = defaultdict(list)
for b,c in df.iterrows():
for d in c.iteritems():
if d[1]>2:
a[b].append(d[0])
print dict(a)
輸出:
{0: ['b'], 1: ['a'], 2: ['a', 'b']}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.