Python

Python: Search strings in a file and get line numbers of lines containing the string

Search strings in a file and get line numbers of lines containing the string in Python.

In this article we will discuss how to search strings in a file and get all the lines and line numbers which will match or which contains the string.

Searching string in a file is easily accomplished by python. And we can easily get the line numbers of lines containing the string.

 Check if string is present in file or not  :

First take a file named “example.txt”

This is an example for sample file
Programming needs logic.
Languages are of many types.
Computer science is a study of computers & computational systems.
We can write a program in any language.
The end

Let’s see the program for it.

#Program :

# create a function to check the string is present in the file or not
def string_in_file(file_name, string_to_search):

  #Checking if any line in the metioned file contains given string or not
  # Open the file in read only mode to read content of the file
  with open(file_name, 'r') as read_obj:

  # Reading all lines in the file one by one by iteration
  for line in read_obj:
  # For each line, checking if the line contains the string or not
    if string_to_search in line:
      return True
  return False



#checking if string 'is' is found in file 'sample.txt'
if string_in_file('sample.txt','is'):
  print('string found')
else:
  print('string not found')
Output:
string found

Here, we use loop for iteration to check each line whether the string is present or not. If the line contains the string then it will return True and if the line does not contain the string then it will return False.

Search for a string in file & get all lines containing the string along with line numbers :

Suppose we have a file named “example.txt”

This is an example for sample file
Programming needs logic.
Languages are of many types.
Computer science is a study of computers & computational systems.
We can write a program in any language.
The end
#Program :

def string_in_file(file_name, string_to_search):
    #Searching for the given string in file along with its line numbers
    line_number = 0
    list_of_results = []
    # Opening the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Reading all lines in the file one by one by iterating the file
        for line in read_obj:
            # checking each line, if the line contains the string
            line_number += 1
            if string_to_search in line:
                # If it contains the string, then add the line number & line as a tuple in the list
                list_of_results.append((line_number, line.rstrip()))
    # Return list of tuples containing line numbers and lines where string is found
    return list_of_results


lines = string_in_file('example.txt', 'is')
print('Total Matched lines : ', len(lines))
for i in lines:
    print('Line Number = ', i[0], ' :: Line = ', i[1])
Output:
Total Matched lines : 2
Line Number =  1  :: Line =  This is an example for sample file
Line Number =  4  :: Line =  Computer science is a study of computers & computational systems.

Here, we use loop for iteration to check each line whether the string is present or not. If the line contains the string then it will return True and if the line does not contain the string then it will return False.

We tried to print the total number of matched lines which consist of the string ‘is’. In total, there where two lines, which include the string ‘is’ and this function returned those lines along with their line numbers. Now instead of searching single string we want to search multiple string.

Search for multiple strings in a file and get lines containing string along with the line numbers :

To search for multiple string in a file, we have to create a separate function, that will open a file once and then search for the lines in the file which contains the given string. Because we cannot use the above created function because this will open and close the file for each string.

Suppose we have a file named “example.txt”

This is an example for sample file
Programming needs logic.
Languages are of many types.
Computer science is a study of computers & computational systems.
We can write a program in any language.
The end
#Program :

def strings_in_file(file_name, list_of_strings):
    #Here getting line from the file along with line numbers
    #which contains any of the matching string from the list
    line_number = 0
    list_of_results = []
    # Opening the file in read only mode
    with open(file_name, 'r') as read_obj:
        # Reading all lines in the file one by one by iteration
        for line in read_obj:
            line_number += 1
            # Checking each line, if the line contains any string from the list of strings
            for string_to_search in list_of_strings:
                if string_to_search in line:
                    # If any string is matched/found in line
                    # then we will append that line along with line number in the list
                    list_of_results.append((string_to_search, line_number, line.rstrip()))
    # Returning the list of tuples containing matched string, line numbers and lines where string is found
    return list_of_results

#Now, we will use this function

# search for given strings in the file 'sample.txt'
matched_lines = strings_in_file('sample.txt', ['is', 'what'])
print('Total Matched lines : ', len(matched_lines))
for elem in matched_lines:
    print('Word = ', elem[0], ' :: Line Number = ', elem[1], ' :: Line = ', elem[2])

Output:
Total Matched lines : 2
Word = 'is' :: Line Number =  1  :: Line =  This is an example for sample file
Word = 'is' :: Line Number =  4  :: Line =  Computer science is a study of computers & computational systems.

Here, we use loop for iteration to check each line whether the string is present or not. If the line contains the string then it will return True and if the line does not contain the string then it will return False.

Python: Search strings in a file and get line numbers of lines containing the string Read More »

Convert 2D NumPy array to list of lists in python

How to convert 2D NumPy array to list of lists in python ?

In this article we will discuss about different ways of converting NumPy array to list of lists in Python. So let’s start exploring the topic.

Converting a 2D Numpy Array to list of lists using tolist() :

In NumPy module of Python, there is a member function tolist() which returns a list congaing the elements of the array. If the array is a 2D array then it returns lists of list.

So, let’s see the implementation of it.

#Program :

import numpy as np
# 2D Numpy array created
arr = np.array([[11, 22, 33, 44],
                [55, 66, 77, 88],
                [12, 13, 23, 43]])

#printing the 2D array                
print(arr)
# Converting 2D Numpy Array to list of lists
list_of_lists = arr.tolist()
#Printing the list of lists
print("The list is")
print(list_of_lists)
Output :
[[11, 22, 33, 44], 
[55, 66, 77, 88], 
[12, 13, 23, 43]]
The list is
[[11, 22, 33, 44], [55, 66, 77, 88], [12, 13, 23, 43]]

Converting a 2D Numpy array to list of lists using iteration :

We can iterate a 2D array row by row and during iteration we can add it to the list. And at the end we can get the list of lists containing all the elements from 2D numpy array.

#Program :

import numpy as np
# 2D Numpy array created
arr = np.array([[11, 22, 33, 44],
                [55, 66, 77, 88],
                [12, 13, 23, 43]])

#printing the 2D array                
print(arr)
# Converting a 2D Numpy Array to list of lists
#iterating row by row using for loop
list_of_lists = list()
for row in arr:
    list_of_lists.append(row.tolist())
#Printing the list of lists
print("The list is")
print(list_of_lists)
Output :
[[11, 22, 33, 44], 
[55, 66, 77, 88], 
[12, 13, 23, 43]]
The list is
[[11, 22, 33, 44], [55, 66, 77, 88], [12, 13, 23, 43]]

Converting a 2D Numpy Array to a flat list :

In both the example, we observed that a 2D NumPy array converted into list of lists. But we can also convert it into a  flat list (not a list of lists) . So, for that first we can convert the 2D NumPy array into 1D Numpy array by using flatten() method.. Then call the tolist() function to convert it into flat list.

#Program :

import numpy as np
# 2D Numpy array created
arr = np.array([[11, 22, 33, 44],
                [55, 66, 77, 88],
                [12, 13, 23, 43]])

#printing the 2D array                
print(arr)

# Converting 2D Numpy array to a flat list
my_list = arr.flatten().tolist()
#Printing the list of lists
print("The list is")
print(my_list)
Output :
[[11, 22, 33, 44], 
[55, 66, 77, 88], 
[12, 13, 23, 43]] 
The list is [11, 22, 33, 44, 55, 66, 77, 88, 12, 13, 23, 43]

Convert 2D NumPy array to list of lists in python Read More »

Python: Three ways to check if a file is empty

Three ways to check if a file is empty in Python.

This article is about to check if a file is empty in Python. We will use three different ways to check if a file is empty or not.

The three ways are

  1. Using os.stat() method.
  2. Using  os.path.getsize() method.
  3. Reading its first character.

Method-1 : Use os.stat() to check if a file is empty :

In python, there is a stat() function which can be used to get the statistics about a file.

os.stat(path, *, dir_fd=None, follow_symlinks=True)

This function

  • It accepts file path (string type) as an argument.
  • It returns an object of the structure stat containing various attributes. (Like st.size to know size of the file)
#Program :

#importing the os module
import os

#file path as string type
path_of_file = 'sample.txt'

# checking if size of file is 0
if os.stat(path_of_file).st_size == 0:
    print('File is empty')
else:
    print('File is not empty')
Output :
File is empty

As our file is empty, so it returned ‘File is empty’.

But we have to be more careful while using it, because if the file does not exist at the respective path then FileNotFoundError.

FileNotFoundError: [WinError 2] The system cannot find the file specified: FILE_NAME

So by keeping that in mind first we will check the file exists or not. We will create a separate function for that. After checking if we will find the file exists then we will check the file is empty or not.

So, let’s see the implementation of it.

#Program :

import os

#Checking the file exists or not
def check_file_empty(path_of_file):
    #Checking if file exist and it is empty
    return os.path.exists(path_of_file) and os.stat(path_of_file).st_size == 0

path_of_file = 'sample.txt'

# checking if file exist and it is empty
EmptyOrNot = check_file_empty(path_of_file)
if EmptyOrNot:
    print('File is empty')
else:
    print('File is not empty')
Output :
File is empty

Method-2 : Check if file is empty using os.path.getsize() in Python :

In Python, os module there is an another function, by using which we can check if a file is empty or not.

os.path.getsize(path)

This function takes file path as an argument and returns the size in bytes. If the file does not exists it gives FileNotFoundError.

#program :

import os

#file path
path_of_file = 'sample.txt'

# checking if size of file is 0
if os.path.getsize(path_of_file) == 0:
    print('File is empty')
else:
    print('File is not empty')
Output : 
File is empty

Method-3 : Check if the file is empty by reading its first character in Python :

We can also check a file is empty or not by reading first character of the file. So, for that it opens the file in read only mode. Then it tries to read first character of the string. If it finds first character in the file then file exists and if it does not find the first character then the file is empty.

#Program :

def file_empty(file_name):
    # open file in read mode
    # Reading first character to check file is empty or not
    with open(file_name, 'r') as read_obj:
        # reading the first character
        one_char = read_obj.read(1)
        # if first character not fpound then file is empty
        if not one_char:
           return True
    return False

path_of_file = 'mysample.txt'
# check if file is empty
EmptyOrNot = file_empty(path_of_file)
print(EmptyOrNot)
Output : 
File is empty

Python: Three ways to check if a file is empty Read More »

Print all Values of a Dictionary

Python: Print all Values of a Dictionary

Dictionaries are Python’s implementation of an associative list, which is a data structure. A dictionary is a collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.

Given a dictionary, the task is to print all the values of the dictionary.

Examples:

Input:

dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}

Output:

200
100
300

Display all values of the Dictionary

There are several ways to display all the values of the dictionary some of them are:

Method #1:Using for loop and values() function

The dictionary class in Python has a function dict.values() that returns an iterable sequence of dictionary values. We can iterate over the sequence of values returned by the function values() using a for loop, and we can print each value while iterating.

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# Traverse the dictionary using for loop
for value in dictionary.values():
    # print the values
    print(value)

Output:

200
100
300

Method #2:Converting values to list

The sequence returned by the values() function can be passed to the list to create a list of all values in the dictionary .

We use list() function to achieve this. Print the list(values)

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# converting values to list
valueslist = list(dictionary.values())
# print the values list
for value in valueslist:
    print(value)

Output:

200
100
300

Method #3:Using  list Comprehension

We can also use this list comprehension to iterate through all of the dictionary’s values and print each one individually.

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# using list comprehension
print([value for value in dictionary.values()])

Output:

[200, 100, 300]

Related Programs:

Python: Print all Values of a Dictionary Read More »

Check if all Elements in a List are Same or Matches a Condition

Python : Check if all Elements in a List are Same or Matches a Condition

A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed.

The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list elements. The same value may appear multiple times in a list. Each event is regarded as a distinct element.

Example:

Input:

givenlist = ['BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks', 'BTechGeeks', ]

Output:

All the elements are equal

Given a list, the task is to check if all elements in a list are same or matches the given condition

Check that all items in a list are identical

There are several ways to check if all elements in a list are same or matches a condition some of them are:

Method #1:Using all() function

any():

If any of the items is True, this function returns True. If the array is empty or all of the values are false, it returns False. Any can be thought of as a series of OR operations on the iterables provided.

Let’s convert the list to Iterable and check that each iterable entry is the same as the first list element using all ()

Below is the implementation:

# given list
givenlist = ['BTechGeeks', 'BTechGeeks',
             'BTechGeeks', 'BTechGeeks', 'BTechGeeks', ]
# checking if all the elements are equal using all() function
if(any(element == givenlist[0] for element in givenlist)):
    print("All the elements are equal")
else:
    print("All the elements are not equal")

Output:

All the elements are equal

Method #2:Using set()

  • Set includes only single elements.
  • Convert the list to set and check whether the length of the set is 1.
  • If the length of given set is 1 the print all the elements are equal
  • else print all the elements are not equal

Below is the implementation:

# given list
givenlist = ['BTechGeeks', 'BTechGeeks',
             'BTechGeeks', 'BTechGeeks', 'BTechGeeks', ]
# converting given list to set
setlist = set(givenlist)
# if the length of given set is 1 then print all the elements are equal
if(len(setlist) == 1):
    print("All the elements are equal")
else:
    print("All the elements are not equal")

Output:

All the elements are equal

Method #3:Using count() function

Count() returns to the list the number of times the item is appeared.

Let’s call the list’s count() function with the first element as an argument. If the number of times it appears in the list equals the length of the list, it means that all of the elements in the list are the same.

Below is the implementation:

# given list
givenlist = ['BTechGeeks', 'BTechGeeks',
             'BTechGeeks', 'BTechGeeks', 'BTechGeeks', ]
# using count function
if(givenlist.count(givenlist[0]) == len(givenlist)):
    print("All the elements are equal")
else:
    print("All the elements are not equal")

Output:

All the elements are equal

Related Programs:

Python : Check if all Elements in a List are Same or Matches a Condition Read More »

Convert Dictionary Values to a List in Python

Convert Dictionary Values to a List in Python

Dictionaries are the implementation by Python of a data structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair.

The list of key value pairs in curly braces that is separated by comma defines a dictionary. Column ‘:’ separates the value of each key.

A dictionary cannot be sorted only to get a representation of the sorted dictionary. Inherently, dictionaries are orderless, but not other types, including lists and tuples. Therefore, you need an ordered data type, which is a list—probably a list of tuples.

Given a dictionary, the task is to convert values of this dictionary to a list.

Examples:

Example 1:

Input:

dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}

Output:

[200, 100, 300]

Example 2:

Input:

dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300 ,'python' :400}

Output:

[200, 100, 300,400]

Convert Dictionary Values to a List in Python

There are several ways to convert dictionary values to a list some of them are:

Method #1:Using list() function

A dictionary class in Python has a function values() that returns an iterable sequence of all dictionary values. The list() function will return a list containing all of the dictionary’s values if we transfer this iterable sequence(dict values) to it.

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# converting values to list
valueslist = list(dictionary.values())
# print the values list
for value in valueslist:
    print(value)

Output:

[200, 100, 300]

Method #2:Using List Comprehension

We can also use this list comprehension to iterate over the values of dictionary and convert to list.

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# converting values to list using list comprehension
valueslist = ([value for value in dictionary.values()])
# print the values list
print(valueslist)

Output:

[200, 100, 300]

Method #3:Convert specific values of dictionary to list

Assume we want to convert only a selected values in a dictionary to a list.

As an example, consider converting values greater than 100.

We can accomplish this by iterating through the dictionary values and checking the condition.

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# Traverse the dictionary values and check condition
valueslist = [value for value in dictionary.values() if value > 100]
# print the values list
print(valueslist)

Output:

[200, 300]

 
Related Programs:

Convert Dictionary Values to a List in Python Read More »

Python Continue keyword in Loops

Python : Continue keyword in Loops

To manage looping specifications, the Python programming language contains the following types of loops. The loops can be executed in three different ways in Python. Although all of the methods have similar basic features, they vary in syntax and the time it takes to search for conditions.

For loops:

In Python, the for loop is used to iterate over a list, tuple, or string, or other iterable properties. Traversal is the process of iterating over a sequence.

While loops:

While Loops are used in Python to execute a set of statements over and over until a condition is met.. When the condition is met, the line immediately following the loop in the programme is executed. Indefinite iteration is demonstrated by the while loop. The term “indefinite iteration” refers to the fact that the number of times the loop is executed is not specified explicitly in advance.

Continue keyword in Python Loops

1)Continue keyword

In Python, loops are used to simplify and repeat tasks in an effective manner. However, there can be occasions when you want to completely exit the loop, miss an iteration, or ignore the condition. Loop control statements can be used to do this. Control statements in loops alter the execution sequence.

Like the break comment, continue is a loop control statement. The continue statement is the polar opposite of the break statement in that it pushes the loop to perform the next iteration rather than ending it.
The continue argument, as its name implies, forces the loop to continue or perform the next iteration. When the continue statement is used in a loop, the code within the loop that follows the continue statement is skipped, and the loop’s next iteration begins.

2)Continue keyword in While loop

The control will jump back to the beginning of the while loop if the continue keyword is used within the loop. For that iteration, all lines following the continue keyword will be skipped.

Consider the following scenario: we want to print all numbers from 1 to n that are not divisible by 5.

Below is the implementation:

# given n
n = 16
# let us take a temp variable to 0 as we need to print all numbers from 1 to n
temp = 0
while(temp < n):
    # increment the temp
    temp += 1
    # if the temp is divisible by 5 then use continue
    # As a result, the number will not be printed.
    if(temp % 5 == 0):
        continue
    else:
        print(temp)

Output:

1
2
3
4
6
7
8
9
11
12
13
14
16

Explanation:

We are printing numbers from 1 to n in this while loop. However, within the loop body, we have a check that says if temp is divisible by 5 we should execute the continue keyword.

As a result, when the value of temp is divisible by 5, the continue statement is executed. It returns control to the beginning of the loop and skips the print statement at the end of the loop body.

3)Continue keyword in For loop

Let the implement the above code using for loop.

Below is the implementation:

# given n
n = 16
# using for loop
for temp in range(1, n+1):
    # if the temp is divisible by 5 then use continue keyword
    # As a result, the number will not be printed.
    if temp % 5 == 0:
        continue
    else:
        print(temp)

Output:

1
2
3
4
6
7
8
9
11
12
13
14
16

Related Programs:

Python : Continue keyword in Loops Read More »

String upper() Method

Python: String upper() Method

A Python string is a collection of characters surrounded by single, double, or triple quotes. The computer does not understand the characters; instead, it stores the manipulated character as a combination of 0’s and 1’s internally.

In this article we are going to discuss the use of upper() function.

String upper() Method in Python

1)Upper() function

The string upper() method returns a string that has all lowercase characters converted to uppercase characters.

Syntax:

given_string.upper()

Parameters:

The upper() method does not accept any parameters.

Return:

The upper() method returns the uppercased version of the given string. All lowercase characters are converted to uppercase.

If no lowercase characters are found, the original string is returned.

2)Converting a string to uppercase

We can convert it using built in function upper().

Below is the implementation:

# given string
string = "btech geeks"
# converting string to upper
string = string.upper()
# print the string
print(string)

Output:

BTECH GEEKS

Explanation:

It returned a copy of the given string, and all lowercase characters were converted to uppercase characters in the returned copy. The uppercase copy of the string was then assigned to the original string.

3)Converting the string which has numbers and letters to uppercase

As the above method we can convert it to uppercase using upper() function.

Below is the implementation:

# given string
string = "btech 2021 geeks"
# converting string to upper
string = string.upper()
# print the string
print(string)

Output:

BTECH 2021 GEEKS

4)Convert two strings to uppercase and compare them

We can convert the given two strings to upper() function and we can compare them using “=” operator.

Below is the implementation:

# given string
string1 = "BTechGeeks"
string2 = "btechgeeks"
# converting both strings to uppercase using upper() function
string1 = string1.lower()
string2 = string2.lower()
# compare the two strings
if(string1 == string2):
    print("Both strings are equal")
else:
    print("Both strings are not equal")

Output:

Both strings are equal

5)Convert character to uppercase

We can convert it using built in function upper().

Below is the implementation:

# given character
character = 'b'
# convert to uppercase using upper() function
character = character.upper()
# print the character
print(character)

Output:

B

Related Programs:

Python: String upper() Method Read More »

How to Remove Duplicates from a List

Python : How to Remove Duplicates from a List

A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed.

The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list elements. The same value may appear multiple times in a list. Each event is regarded as a distinct element.

Example:

Input:

givenlist = ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']

Output:

Given list with duplicates :  ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']
Given list without duplicates :  ['hello', 'this', 'is', 'BTechGeeks']

Delete Duplicates elements from the list

We can delete duplicate elements by many methods some of them are:

Method #1:Using set

  • A set is an unordered data structure with only unique elements.
  • So, using the set() function, convert the given list to a set and then back to a list.
  • Print the list

Below is the implementation:

# given list (which contains duplicates)
givenlist = ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']
print('Given list with duplicates : ', givenlist)
# converting givenlist to set and then back to list to remove duplicates
givenlist = list(set(givenlist))
# print given list
print('Given list without duplicates : ', givenlist)

Output:

Given list with duplicates :  ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']
Given list without duplicates :  ['this', 'is', 'hello', 'BTechGeeks']

Note:

While this method avoids duplicates, it does not hold the elements in the same order as the original.

Method #2:Using in operator and lists (same order)

For unique elements, we’ll need to make a new list. Then iterate through the original list, adding each element to the newList only if it isn’t already there.

Below is the implementation: 

# given list (which contains duplicates)
givenlist = ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']
print('Given list with duplicates : ', givenlist)
# taking empty list
newlist = []
# Travers the given list
for element in givenlist:
    # if the element is not present in new list then append it
    if element not in newlist:
        newlist.append(element)
# print new list
print('Given list without duplicates : ', newlist)

Output:

Given list with duplicates :  ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']
Given list without duplicates :  ['hello', 'this', 'is', 'BTechGeeks']

Method #3:Using Counter() function (same order)

  • Counter() function is mainly used to calculate the frequencies of all elements .
  • In this case we calculate the frequencies using Counter function which returns dictionary having unique keys.
  • Convert this keys to list and print it.

Below is the implementation:

from collections import Counter
# given list (which contains duplicates)
givenlist = ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']
print('Given list with duplicates : ', givenlist)
# counting the frequencies using Counter function
freq = Counter(givenlist)
# converting keys to list
uniquelist = list(freq.keys())
# print unqiue list
print('Given list without duplicates : ', uniquelist)

Output:

Given list with duplicates :  ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']
Given list without duplicates :  ['hello', 'this', 'is', 'BTechGeeks']

 
Related Programs:

Python : How to Remove Duplicates from a List Read More »

Pandas : count rows in a dataframe | all or those only that satisfy a condition

Count all rows or those that satisfy some condition in Pandas dataframe

In this article we are going to show you how to count number of all rows in a DataFrame or rows that satisfy given condition in it.

First we are going to create dataframe,

import pandas as pd
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
details = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],
          index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])

print(details)

Output:

RESTART: C:/Users/HP/Desktop/dataframe.py
Name       Age   Place      College
a   Ankit         22     Up          Geu
b   Ankita       31    Delhi       Gehu
c    Rahul       16    Tokyo      Abes
d   Simran     41     Delhi      Gehu
e   Shaurya    33     Delhi      Geu
f    Harshita   35     Mumbai Bhu
g   Swapnil    35     Mp        Geu
i    Priya         35     Uk         Geu
j    Jeet          35     Guj        Gehu
k   Ananya     35    Up         Bhu

Now lets see some other methods to count the rows in dataframe.

Count all rows in a Pandas Dataframe using Dataframe.shape

Dataframe.shape attribute gives a sequence of index or row labels

(Number_of_index, Number_of_columns)

Number of index basically means number of rows in the dataframe.Let’s use this to count number of rows in above created dataframe.

import pandas as pd
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
details = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],
          index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])
numOfRows = details.shape[0]

print("Number of rows :",numOfRows)

Output:

RESTART: C:/Users/HP/Desktop/dataframe.py
Number of rows : 10

Count all rows in a Pandas Dataframe using Dataframe.index

Dataframe.index attribute gives a sequence of index or row labels.We can calculate the length of that sequence to find out the number of rows in the dataframe.

import pandas as pd
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
details = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],
          index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])
numOfRows = len(details.index)
print('Number of Rows in dataframe : ' , numOfRows)

Output:

RESTART: C:/Users/HP/Desktop/dataframe.py

Number of rows : 10

Count rows in a Pandas Dataframe that satisfies a condition using Dataframe.apply()

This function apply  to all the rows of a dataframe to find out if elements of rows satisfies a condition or not, Based on the result it returns a bool series.

import pandas as pd
students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', 31, 'Delhi', 'Gehu'),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', 33, 'Delhi', 'Geu'),
           ('Harshita', 35, 'Mumbai', 'Bhu' ),
           ('Swapnil', 35, 'Mp', 'Geu'),
           ('Priya', 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           ('Ananya', 35, 'Up', 'Bhu')
            ]
details = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],
          index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])
seriesObj = details.apply(lambda x: True if x['Age'] > 30 else False , axis=1)
numOfRows = len(seriesObj[seriesObj == True].index)
print('Number of Rows in dataframe in which Age > 30 : ', numOfRows)

Output:

RESTART: C:/Users/HP/Desktop/dataframe.py
Number of Rows in dataframe in which Age > 30 : 8

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas

Conclusion:

In this article we have seen different method to count rows in a dataframe  all or those  that satisfy a condition.

Happy learning guys.

Pandas : count rows in a dataframe | all or those only that satisfy a condition Read More »