我有一张桌子:
我想切前三列,我该怎么做?
我已经查看了另一篇文章:Finding top N columns for each row in data frame,但这对我来说太过分了。
我试过了:
df1 = df.iloc[:,0:3]
但这会产生错误: IndexingError:索引器太多
修改
添加了 ix
的更详细代码 cols = [col for col in df.columns if col != 'stream']
candidates = df.loc[url,cols]
dfSorted = candidates.sort_values(ascending=False)
big_three = dfSorted.ix[:,0:3]
答案 0 :(得分:1)
我认为您可以使用apply
与nlargest
一起选择3
DataFrame
的前candidates
列和值:
import pandas as pd
df = pd.DataFrame({'A': {'a': 1, 'c': 5, 'b': 2, 'd': 3},
'C': {'a': 8, 'c': 7, 'b': 8, 'd': 7},
'B': {'a': 4, 'c': 1, 'b': 5, 'd': 4},
'D': {'a': 5, 'c': 3, 'b': 9, 'd': 1},
'stream': {'a': 1, 'c': 2, 'b': 2, 'd': 3}})
print df
A B C D stream
a 1 4 8 5 1
b 2 5 8 9 2
c 5 1 7 3 2
d 3 4 7 1 3
cols = [col for col in df.columns if col != 'stream']
candidates = df.ix['a':'c', cols]
print candidates
A B C D
a 1 4 8 5
b 2 5 8 9
c 5 1 7 3
print candidates.apply(lambda x: zip(x.nlargest(3).index, x.nlargest(3).values), axis=1)
a [(C, 8), (D, 5), (B, 4)]
b [(D, 9), (C, 8), (B, 5)]
c [(C, 7), (A, 5), (D, 3)]
dtype: object
与...相同:
def f(x):
#print x.nlargest(3)
#print zip(x.nlargest(3).index, x.nlargest(3).values)
return zip(x.nlargest(3).index, x.nlargest(3).values)
print candidates.apply(f, axis=1)
a [(C, 8), (D, 5), (B, 4)]
b [(D, 9), (C, 8), (B, 5)]
c [(C, 7), (A, 5), (D, 3)]
dtype: object