How to count rows without using pandas in python?

how to count rows without using pandas in python? lets who is python champion

2 Likes

Welcome to the group.

Your question doesn’t seem to be very detailed. What sort of objects having rows need counting? I can think of a few off the top of my head:

  • Numpy array
  • List of lists
  • List of strings
  • SQL query result
  • Text file

and so on.

Hello, @irfanshaikh, and welcome to the Python Forum!

We can provide better help on the Forum if you provide us with details about what you are trying to do. These details might include any of the following:

  • The Python code you have been working on
  • A detailed description of the problem you are trying to solve
  • A copy of part of a data file that you are using

From the information that you have provided, which mentions pandas, we might guess that you are working with a data file. Here is a simple example of contents from a data file named "cats.txt".:

common name,genus,species
Leopard,Panthera,pardus
Lion,Panthera,leo
Panther,Panthera,tigris
Jaguar,Panthera,onca
Cat,Felis,domesticus
Ocelot,Leopardus,pardaliscatus

Notice that it contains a header and six rows of data.

The following simple Python program displays the number of rows, including the header:

# cats.py
data_file = open("cats.txt")
rows = data_file.readlines()
data_file.close()
print(len(rows)) # number of rows, including header

Output:

7

We look forward to learning more about your project, so that we can offer additional help, and to seeing you on the Python Forum in the future.

hello
I don’t know much about it.But I think you can try this:

import pandas as pd

#initialize dataframe
df = pd.DataFrame({'a': [1, 4, 7, 2], 'b': [2, 0, 8, 7]})

#number of rows in dataframe
num_rows = df.shape[0]

print('Number of Rows in DataFrame :',num_rows)

Run

1 Like

Oh, :bulb:, it is a Python contest.

To begin, we need some data. If you upload a data file for us here, we will all try to open it and count its rows with Python, but without pandas. :smile:

1 Like