pandas 前 n 列数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36981873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:09:32  来源:igfitidea点击:

First n columns data frame

pythonpandas

提问by Stanko

I have a table:

我有一张桌子:

simple_table

简单表

And I would like to slice the first three columns, how can I do this?

我想对前三列进行切片,我该怎么做?

I've looked at this other post: Finding top N columns for each row in data frame, but that is overkill for me.

我看过另一篇文章:为数据框中的每一行查找前 N 列,但这对我来说太过分了。

I've tried:

我试过了:

df1 = df.iloc[:,0:3]

But this gives the error: IndexingError: Too many indexers

但这给出了错误:IndexingError: Too many indexers

EDIT:

编辑:

More detailed code with the added ix

添加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]

回答by jezrael

I think you can use applywith nlargestfor selecting top 3columns and values of DataFramecandidates:

我认为您可以使用applywithnlargest来选择顶部的3列和值DataFramecandidates

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

What is same as:

什么是相同的:

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