是否有一个 Pandas 函数来显示前 n 列/后 n 列,如 .head() 和 .tail()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30608310/
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-13 23:25:31  来源:igfitidea点击:

Is there a pandas function to display the first/last n columns, as in .head() & .tail()?

pythonpandas

提问by MMelnicki

I love using the .head()and .tail()functions in pandas to circumstantially display a certain amount of rows (sometimes I want less, sometimes I want more!). But is there a way to do this with the columns of a DataFrame?

我喜欢在 Pandas 中使用.head().tail()函数来临时显示一定数量的行(有时我想要更少,有时我想要更多!)。但是有没有办法用 DataFrame 的列来做到这一点?

Yes, I know that I can change the display options, as in: pd.set_option('display.max_columns', 20)

是的,我知道我可以更改显示选项,例如: pd.set_option('display.max_columns', 20)

But that is too clunky to keep having to change on-the-fly, and anyway, it would only replace the .head()functionality, but not the .tail()functionality.

但这太笨重了,无法继续动态更改,无论如何,它只会替换.head()功能,而不是.tail()功能。

I also know that this could be done using an accessor: yourDF.iloc[:,:20]to emulate .head(20) and yourDF.iloc[:,-20:]to emulate .tail(20).

我也知道这可以使用访问器来完成: yourDF.iloc[:,:20]模拟 .head(20) 和yourDF.iloc[:,-20:]模拟 .tail(20)。

It may look like a short amount of code, but honestly it's not as intuitive nor swift as when I use .head().

它可能看起来像一小段代码,但老实说,它不像我使用 .head() 时那么直观和快捷。

Does such a command exist? I couldn't find one!

有这样的命令吗?我找不到一个!

采纳答案by unutbu

No, such methods are not supplied by Pandas, but it is easy to make these methods yourself:

不,这样的方法不是由 Pandas 提供的,但是自己制作这些方法很容易:

import pandas as pd
def front(self, n):
    return self.iloc[:, :n]

def back(self, n):
    return self.iloc[:, -n:]

pd.DataFrame.front = front
pd.DataFrame.back = back

df = pd.DataFrame(np.random.randint(10, size=(4,10)))

So that now allDataFrame would possess these methods:

这样现在所有的DataFrame 都将拥有这些方法:

In [272]: df.front(4)
Out[272]: 
   0  1  2  3
0  2  5  2  8
1  9  9  1  3
2  7  0  7  4
3  8  3  9  2

In [273]: df.back(3)
Out[273]: 
   7  8  9
0  3  2  7
1  9  9  4
2  5  7  1
3  3  2  5

In [274]: df.front(4).back(2)
Out[274]: 
   2  3
0  2  8
1  1  3
2  7  4
3  9  2


If you put the code in a utility module, say, utils_pandas.py, then you can activate it with an import statement:

如果您将代码放在实用程序模块中,例如utils_pandas.py,,那么您可以使用导入语句激活它:

import utils_pandas

回答by vk1011

Closest emulation, which you could put in a function:

最接近的仿真,您可以将其放入函数中:

number_of_columns = 5 # eg.
head_cols = df[df.columns[:number_of_columns]]
tail_cols = df[df.columns[-number_of_columns:]]

回答by Alex Ferrer

Transpose it to use head and go back

转置它以使用头部并返回

df.T.head().T

to avoid index slicing or custom methods.

以避免索引切片或自定义方法。

回答by nitin

You could just use df.col.head(n) for what your are trying to do... see example below,

你可以只使用 df.col.head(n) 来做你想做的事情……见下面的例子,

df = pd.DataFrame({'a': [i for i in range(101)],
                   'b': [i for i in range(101)]})
df.a.head(4)

Out[37]:
0    0
1    1
2    2
3    3
Name: a, dtype: int64