19

How do you print (in the terminal) a subset of columns from a pandas dataframe?

I don't want to remove any columns from the dataframe; I just want to see a few columns in the terminal to get an idea of how the data is pulling through.

Right now, I have print(df2.head(10)) which prints the first 10 rows of the dataframe, but how to I choose a few columns to print? Can you choose columns by their indexed number and/or name?

3
  • 1
    This page would be helpful : pandas.pydata.org/pandas-docs/stable/indexing.html Commented Aug 8, 2017 at 20:40
  • 3
    I do df.iloc[:5, :5] Commented Aug 8, 2017 at 20:41
  • 1
    df.sample(n=4, axis=1) would give you random sample of columns. \\ This question is not a duplicate and the link provided to other question does not answer the question as stated in the title (I got here looking for precisely the title question) even though it may fulfill the questioner's purpose as stated in the question body. Commented Jan 8, 2021 at 22:39

1 Answer 1

45

print(df2[['col1', 'col2', 'col3']].head(10)) will select the top 10 rows from columns 'col1', 'col2', and 'col3' from the dataframe without modifying the dataframe.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.