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?
Yes, I know that I can change the display options, as in:
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.
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).
It may look like a short amount of code, but honestly it's not as intuitive nor swift as when I use .head().
Does such a command exist? I couldn't find one!
No, such methods are not supplied by Pandas, but it is easy to make these methods yourself:
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 all DataFrame would possess these methods:
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:
import utils_pandas
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:]]
Transpose it to use head and go back
df.T.head().T
to avoid index slicing or custom methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With