Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 133 of 855
How to Replace Values in Columns Based on Condition in Pandas
In Python, we can replace values in columns based on conditions in Pandas using various built-in functions like loc, where and mask, apply and lambda, map, and numpy.where. Pandas is a powerful library for data manipulation and working with structured data. This article demonstrates five different methods to conditionally replace column values. Using loc The loc function allows you to access and modify specific rows and columns in a DataFrame based on boolean conditions ? Syntax df.loc[row_condition, column_labels] = new_value Example Let's replace the gender of people aged 50 or older with ...
Read MoreFaulty calculator using Python
A faulty calculator in Python is a calculator that gives incorrect results for certain calculations. In Python, we can create our own calculator and use it for doing mathematical calculations. If we want to create a faulty calculator we need to create or introduce errors in the functions that perform the calculations. In this article, we will create a faulty calculator using Python. Creating a Faulty Calculator Creating a faulty calculator is easy as we need to just introduce some incorrect calculations in the normal calculator in the code to give an incorrect result which converts it into ...
Read MoreHow to replace a word in Excel using Python?
In Python, we can replace a word in Excel with another word using a third-party Python library called openpyxl. Microsoft Excel is a useful tool that is used for managing and analyzing data. Using Python we can automate some of the Excel data management tasks. In this article, we will understand how we can replace a word in Excel using Python. Installing openpyxl Before implementing the program to replace words in Excel we need to install the openpyxl library in our system using the Python package manager. To install openpyxl type the following command on your terminal or ...
Read MorePython Program to Find the Number of Unique Words in Text File
Finding the number of unique words in a text file is a common text processing task. Python provides several approaches to accomplish this, with sets and dictionaries being the most efficient methods. Sample Text File Content For demonstration, we'll create a sample text file with repeated content ? # Create a sample text file sample_text = """This is a new file. This is made for testing purposes only. There are four lines in this file. There are four lines in this file. There are four lines in this file. There are four lines in this file. ...
Read MorePython program to find the frequency of the elements which are common in a list of strings
In this Python article, we explore how to find the frequency of elements that are common in a list of strings. We'll demonstrate three different approaches: finding character frequency using reduce(), word frequency using list operations, and word frequency using pandas functions. Example 1: Character Frequency Using reduce() Function This approach finds the frequency of characters that appear in all strings using the reduce() function with Counter ? from functools import reduce from collections import Counter # Sample list of strings strings = ['Types of Environment - Class Discussion', ...
Read MorePython Program to Find Sum of First and Last Digit
In this article, we explore how to find the sum of the first and last digits of an integer. This task requires extracting the first digit (leftmost) and the last digit (rightmost) from any number, then adding them together. We'll demonstrate four different approaches to solve this problem. Method 1: Using Repeated Division to Count Digits This approach counts digits by repeatedly dividing the number by 10, then extracts the first digit using division and the last digit using modulo ? import math def count_digits(number): count = 0 ...
Read MorePython Program to Find Unique Lines From Two Text Files
Finding unique lines between two text files is a common task in data processing and file comparison. Python provides several approaches to accomplish this, from basic iteration to using specialized libraries. In this article, we'll explore three different methods to identify lines that exist in one file but not the other. Sample Data For our examples, we'll work with two text files containing course names. Here's the content comparison ? Course Name In a.txt In b.txt Introduction to Computers Yes Yes Introduction to Programming Concepts Yes Yes Introduction to ...
Read MorePython program to find start and end indices of all Words in a String
Finding the start and end indices of words in a string is useful for text processing, highlighting, and analysis. Python provides multiple approaches: manual iteration through characters or using specialized libraries like NLTK. Method 1: Using Manual Iteration This approach iterates through each character, detecting spaces to identify word boundaries ? Algorithm Step 1 − Create a function that iterates through the string character by character. Step 2 − Track the starting index of each word and detect spaces to find ending indices. Step 3 − Handle the last word separately since it doesn't end with ...
Read MorePython Program to Get Sum of N Armstrong Numbers
An Armstrong number (also called a narcissistic number) is a number where the sum of its digits raised to the power of the total number of digits equals the number itself. For example, 153 is a 3-digit Armstrong number because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. This tutorial shows how to find and sum all Armstrong numbers of a given digit length using Python. What is an Armstrong Number? Let's first understand the concept with a simple function to check if a number is an Armstrong number ? ...
Read MorePython Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range
Finding numbers divisible by 7 and multiples of 5 within a range is a common programming problem. Python provides several approaches: using the modulo operator, iterative addition, and multiplication methods. Each approach has different performance characteristics and use cases. Method 1: Using Modulo Operator The most straightforward approach uses the modulo operator (%) to check divisibility. A number is divisible by 7 if number % 7 == 0, and a multiple of 5 if number % 5 == 0. Algorithm Step 1 − Define the range with lower and upper bounds. Step 2 − Iterate through ...
Read More