python - Is there a pandas function to display the first/last n columns, as in .head() & .tail()? -
i love using .head()
, .tail()
functions in pandas circumstantially display amount of rows (sometimes want less, want more!). there way columns of dataframe?
yes, know can change display options, in: pd.set_option('display.max_columns', 20)
but clunky keep having change on-the-fly, , anyway, replace .head()
functionality, not .tail()
functionality.
i know done using accessor: yourdf.iloc[:,:20]
emulate .head(20) , yourdf.iloc[:,-20:]
emulate .tail(20).
it may short amount of code, it's not intuitive nor swift when use .head().
does such command exist? couldn't find one!
no, such methods not supplied pandas, easy make these methods yourself:
import pandas pd def front(self, n): return self.iloc[:, :n] def back(self, n): return self.iloc[:, -n:] pd.dataframe.front = front pd.dataframe.back = df = pd.dataframe(np.random.randint(10, size=(4,10)))
so all dataframe 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 put code in utility module, say, utils_pandas.py
, can activate import statement:
import utils_pandas
Comments
Post a Comment