Kontext Kontext

Pandas DataFrame - Get Row and Column Count

event 2022-07-07 visibility 197 comment 0 insights toc
more_vert
insights Stats

Pandas DataFrame is one of the most commonly used class in data programming with Python. This article shows you how to get row and column count of a pandas DataFrame.

Get row count

There are multiple ways to get row count.

Via index

Use len(df.index) to get the row count.

>>> import pandas as pd
>>> mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
...           {'a': 100, 'b': 200, 'c': 300, 'd': 400},
...           {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
>>> df = pd.DataFrame(mydict)
>>> df
      a     b     c     d
0     1     2     3     4
1   100   200   300   400
2  1000  2000  3000  4000
>>> row_count = len(df.index)
>>> row_count
3

Via shape

We can also use df.shape attribute to get the row count.

>>> df.shape[0]
3

Via columns

We could also use any column to get the row count however it only returns the count of non-NaN values.

>>> df[df.columns[0]].count()
3

Get column count

For shape attribute of DataFrame, it also returns the column count.

>>> df.shape[1]
4
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts