How do I get the row count of a pandas DataFrame?

yemu

I'm trying to get the number of rows of dataframe df with Pandas, and here is my code.

Method 1:

total_rows = df.count
print total_rows +1

Method 2:

total_rows = df['First_columnn_label'].count
print total_rows +1

Both the code snippets give me this error:

TypeError: unsupported operand type(s) for +: 'instancemethod' and 'int'

What am I doing wrong?

root

You can use the .shape property or just len(DataFrame.index). However, there are notable performance differences ( len(DataFrame.index) is fastest).

enter image description here


Code to reproduce the plot:

import numpy as np
import pandas as pd
import perfplot


perfplot.save(
    "out.png",
    setup=lambda n: pd.DataFrame(np.arange(n * 3).reshape(n, 3)),
    n_range=[2**k for k in range(25)],
    kernels=[
        lambda data: data.shape[0],
        lambda data: data[0].count(),
        lambda data: len(data.index),
    ],
    labels=["data.shape[0]", "data[0].count()", "len(data.index)"],
    xlabel="data rows"
)

EDIT: As @Dan Allen noted in the comments len(df.index) and df[0].count() are not interchangeable as count excludes NaNs,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get count of a row in SQL

From Dev

How do I remove/omit the count column from the dataframe in Pandas?

From Dev

How do I get the average row count per day of data?

From Dev

How do I get updated row count using sqlite in Android?

From Dev

How do i find the iloc of a row in pandas dataframe?

From Dev

How do I extend a pandas DataFrame by repeating the last row?

From Dev

How do I add one row of a PANDAS dataframe to the rest of the rows?

From Dev

How do I copy a row from one pandas dataframe to another pandas dataframe?

From Dev

How do I get the maximum within a subset of my dataframe in Pandas?

From Dev

How can i get gridview row count

From Java

How to get previous row with condition in a DataFrame of Pandas

From Dev

How to get row delta value by pandas dataframe

From Dev

How to get row number in dataframe in Pandas?

From Dev

How to get row delta value by pandas dataframe

From Dev

pandas dataframe count row values

From Dev

Using Python how do I generate a random number within a range for each row in Pandas dataframe?

From Dev

With a pandas DataFrame, how do I keep every 7th row?

From Dev

Using Python how do I generate a random number within a range for each row in Pandas dataframe?

From Dev

How can I get the timezone adjusted hour for each row in a pandas DataFrame when working with multiple timezones?

From Java

How do I get the count of a Swift enum?

From Java

How do I get the Git commit count?

From Dev

How do I get a summary count of missing/NaN data by column in 'pandas'?

From Dev

How do I get a summary count of missing/NaN data by column in 'pandas'?

From Dev

How do I columnwise reduce a pandas dataframe?

From Dev

How do I write a pandas dataframe to Vertica?

From Dev

How do I reshape or pivot a DataFrame in Pandas

From Dev

How do i sort the values in a dataframe in pandas?

From Dev

How can I get this series to a pandas dataframe?

From Dev

How do I stack rows in a Pandas data frame to get one "long row"?

Related Related

  1. 1

    How do I get count of a row in SQL

  2. 2

    How do I remove/omit the count column from the dataframe in Pandas?

  3. 3

    How do I get the average row count per day of data?

  4. 4

    How do I get updated row count using sqlite in Android?

  5. 5

    How do i find the iloc of a row in pandas dataframe?

  6. 6

    How do I extend a pandas DataFrame by repeating the last row?

  7. 7

    How do I add one row of a PANDAS dataframe to the rest of the rows?

  8. 8

    How do I copy a row from one pandas dataframe to another pandas dataframe?

  9. 9

    How do I get the maximum within a subset of my dataframe in Pandas?

  10. 10

    How can i get gridview row count

  11. 11

    How to get previous row with condition in a DataFrame of Pandas

  12. 12

    How to get row delta value by pandas dataframe

  13. 13

    How to get row number in dataframe in Pandas?

  14. 14

    How to get row delta value by pandas dataframe

  15. 15

    pandas dataframe count row values

  16. 16

    Using Python how do I generate a random number within a range for each row in Pandas dataframe?

  17. 17

    With a pandas DataFrame, how do I keep every 7th row?

  18. 18

    Using Python how do I generate a random number within a range for each row in Pandas dataframe?

  19. 19

    How can I get the timezone adjusted hour for each row in a pandas DataFrame when working with multiple timezones?

  20. 20

    How do I get the count of a Swift enum?

  21. 21

    How do I get the Git commit count?

  22. 22

    How do I get a summary count of missing/NaN data by column in 'pandas'?

  23. 23

    How do I get a summary count of missing/NaN data by column in 'pandas'?

  24. 24

    How do I columnwise reduce a pandas dataframe?

  25. 25

    How do I write a pandas dataframe to Vertica?

  26. 26

    How do I reshape or pivot a DataFrame in Pandas

  27. 27

    How do i sort the values in a dataframe in pandas?

  28. 28

    How can I get this series to a pandas dataframe?

  29. 29

    How do I stack rows in a Pandas data frame to get one "long row"?

HotTag

Archive