1

I would like to list all the columns, and the number of rows in a Pandas data.frame. For example, suppose that my data frame df looks like:

    field1   field2
 x    x1        1
 y    y1        4

I would like to be able to run:

> df.columns()
['field1','field2']
> df.nrows() 
2

Is that possible?

2
  • umm, wouldn't len(yourFrame) give you the number of rows? Commented Feb 11, 2014 at 16:32
  • 1
    Yes, use df.columns and df.shape[0] Commented Feb 11, 2014 at 16:33

1 Answer 1

2

Name of columns: a Pandas dataframe has a columns attribute (it's not a function). To get a regular list of columns as strings:

>>> [name for name in df.columns]
['field1','field2']

To get number of rows:

>>> len(df.index)
2
Sign up to request clarification or add additional context in comments.

1 Comment

If I print df.index, I'm just getting a list of my index values. As per the example, how can I get x1, y1, z1, etc. in a list?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.