15

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?

daniellemarie
  • 151
  • 1
  • 1
  • 4
  • 1
    This page would be helpful : https://pandas.pydata.org/pandas-docs/stable/indexing.html – Spandan Brahmbhatt Aug 08 '17 at 20:40
  • 3
    I do `df.iloc[:5, :5]` – ayhan Aug 08 '17 at 20:41
  • ```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. – rahul-ahuja Jan 08 '21 at 22:39

1 Answers1

35

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.

RagingRoosevelt
  • 2,046
  • 19
  • 34