我有以下数据框:
df1 = pd.DataFrame(data={'1': ['a', 'd', 'g', 'j'],
'2': ['b', 'e', 'h', 'k'],
'3': ['c', 'f', 'i', 'l'],
'top_n': [1, 3, 2, 1]},
index=pd.Series(['ind1', 'ind2', 'ind3', 'ind4'], name='index'))
>>> df1
1 2 3 top_n
index
ind1 a b c 1
ind2 d e f 3
ind3 g h i 2
ind4 j k l 1
如何基于top_n
列获取每行的前N个值?
>>> df1
1 2 3 top_n
index
ind1 a NaN NaN 1
ind2 d e f 3
ind3 g h NaN 2
ind4 j NaN NaN 1
在这个例子中,
ind3
拥有 g
和 h
,因为 top_n
值为 2。