I have a dataframe that in certain cases may grow to a large number of columns. Sometimes this number of columns grows to be too much for display purposes. What I would like to do is say that the dataframe may have no more than 16 columns and if it does just truncate the extra columns.
Pandas has a truncate function. By default it works on rows, but it can also work on columns. It looks like df.truncate(before="A", after="B", axis="columns")
assuming that both A and B are columns among others in the dataframe. However, if I go like df.truncate(before=1, after=16, axis="columns")
I get a slicing problem because it wants column names apparently.
Is there a way to remove extra columns without knowing or caring about the names of the columns? This is important because it is general purpose code that may be used for different, but related data.
I tried to use truncate function and I was expecting to be able to limit number of columns by a fixed constant.
If your column names are unique, you could go with:
df.truncate(before=df.columns[1], after=df.columns[16], axis="columns")