Python Articles

Page 31 of 855

Python - Replace negative value with zero in numpy array

Kalyan Mishra
Kalyan Mishra
Updated on 27-Mar-2026 3K+ Views

In NumPy arrays, replacing negative values with zero is a common preprocessing step in data analysis. Python offers several efficient methods to accomplish this task, from basic list comprehension to NumPy's built-in functions. Using List Comprehension This approach converts the array to a list, applies conditional logic, and converts back to NumPy array − import numpy as np arr = np.array([-12, 32, -34, 42, -53, 88]) result = np.array([0 if x < 0 else x for x in arr]) print("Original array:", arr) print("Modified array:", result) Original array: [-12 32 -34 ...

Read More

Python Replace NaN values with average of columns

Kalyan Mishra
Kalyan Mishra
Updated on 27-Mar-2026 714 Views

In this article, we will explore methods to replace NaN (Not a Number) values with the average of columns. When working with data analysis, handling NaN values is a crucial step. Here you will learn various approaches to replace NaN values with column averages using NumPy. Using numpy.nanmean() and numpy.where() The most straightforward approach uses nanmean() to calculate column averages and where() to replace NaN values ? import numpy as np arr = np.array([[1, 2, np.nan], [4, np.nan, 6], ...

Read More

Python - Replace multiple characters at once

Kalyan Mishra
Kalyan Mishra
Updated on 27-Mar-2026 8K+ Views

In this article we will see methods to replace multiple characters at once in any string. When working with programs, we often face situations where we need to replace particular characters simultaneously at all their occurrences. Suppose you have text containing the word "word" with multiple occurrences and you want to convert "word" to "work" − doing this manually one by one is inefficient. Let's explore multiple methods to solve this problem efficiently. Using str.replace() Method This is the most straightforward method using Python's built-in replace function to replace desired characters ? Example string = ...

Read More

Python - Replace list elements with its ordinal number.

Kalyan Mishra
Kalyan Mishra
Updated on 27-Mar-2026 427 Views

In this article, we will learn how to replace list elements with their ordinal numbers (position-based indices). An ordinal number represents the position of an item in a sequence, starting from 0 in programming. For example, in the list [6, 8, 5, 3], the ordinal values are: Element 6 has ordinal value 0 (first position) Element 8 has ordinal value 1 (second position) Element 5 has ordinal value 2 (third position) Element 3 has ordinal value 3 (fourth position) Let's explore various methods to replace nested list elements with their ordinal numbers. Using List ...

Read More

Jacobian matrix in PyTorch

Kalyan Mishra
Kalyan Mishra
Updated on 27-Mar-2026 492 Views

In this article we will learn about the Jacobian matrix and how to calculate this matrix using different methods in PyTorch. We use Jacobian matrix in various machine learning applications. What is a Jacobian Matrix? The Jacobian matrix is a mathematical tool used to calculate the relationship between input and output variables. It contains all the partial derivatives of a vector-valued function. This matrix has various applications in machine learning and computational mathematics: Analyzing gradients and derivatives of functions in multivariable calculus Solving differential equations of systems Calculating inverse of vector-valued functions Analyzing stability of dynamic ...

Read More

Lumped Capacitance Analysis using Python

Dr Pankaj Dumka
Dr Pankaj Dumka
Updated on 27-Mar-2026 484 Views

Lumped capacitance analysis is used when an object at high temperature is suddenly placed in a cooler medium. If the conductive resistance of the solid is much smaller than the convective resistance, we can treat the object as having uniform temperature throughout (a "lump"). The rate of internal energy change equals the heat transfer to the surrounding fluid. Hot Object T(t) Surrounding Fluid T∞ (constant) ...

Read More

Implementation of Jacobi Method to Solve a System of Linear Equations in Python

Dr Pankaj Dumka
Dr Pankaj Dumka
Updated on 27-Mar-2026 3K+ Views

The Jacobi Method is an iterative algorithm for solving systems of linear equations. It starts with initial guesses and repeatedly refines them until convergence is achieved. Mathematical Foundation Consider a system of linear equations: a₁₁x₁ + a₁₂x₂ + ... + a₁ₙxₙ = b₁ a₂₁x₁ + a₂₂x₂ + ... + a₂ₙxₙ = b₂ ... aₙ₁x₁ + aₙ₂x₂ + ... + aₙₙxₙ = bₙ The Jacobi method rearranges each equation to isolate one variable. For the i-th equation: xi = (bi - Σ(aijxj)) / aii The Jacobi Algorithm The algorithm follows these steps: ...

Read More

How to Concatenate Column Values in a Pandas DataFrame?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 8K+ Views

Pandas is a powerful library for data manipulation and analysis in Python. Concatenating column values involves combining the values of two or more columns into a single column, which is useful for creating new variables, merging data from different sources, or formatting data for analysis. There are several methods to concatenate column values in a Pandas DataFrame. In this tutorial, we'll explore two common approaches: using the str.cat() method and using string concatenation with operators. Using the str.cat() Method The str.cat() method is designed specifically for concatenating string values in pandas Series. It provides clean syntax and ...

Read More

How to Collapse Multiple Columns in Python Pandas?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 3K+ Views

Pandas is a popular data manipulation library in Python that is widely used for working with structured data. One common task when working with data is to clean and transform it in order to prepare it for analysis. Sometimes, the data might contain multiple columns that have similar information or are related to each other. In such cases, it might be useful to collapse these columns into a single column for easier analysis or visualization. Pandas provides several methods to collapse multiple columns into a single column. In this tutorial, we will explore the two most common methods: ...

Read More

Circle of Squares using Python Turtle

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 3K+ Views

The Circle of Squares is a fascinating geometric pattern that can be created using Python's turtle graphics library. This pattern consists of a circle of squares that are evenly spaced around its circumference, with each square rotated at an angle relative to the previous square. This creates a mesmerizing visual effect that can be customized to suit any color scheme or size. In this tutorial, we will explore how to create the Circle of Squares pattern using Python's turtle library, step by step. We will also discuss different customization options that can be applied to create unique variations of ...

Read More
Showing 301–310 of 8,549 articles
« Prev 1 29 30 31 32 33 855 Next »
Advertisements