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

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

Transpose it to use head and go back

df.T.head().T

to avoid index slicing or custom methods.

Tags:

Python

Pandas