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
Server Side Programming Articles
Page 81 of 2109
How to Use axis=0 and axis=1 in Pandas?
In Pandas, the axis parameter controls the direction of operations on DataFrames and Series. Understanding axis=0 and axis=1 is essential for performing row-wise and column-wise operations efficiently. Understanding Axis in Pandas A Pandas DataFrame is a two-dimensional table with rows and columns. The axis parameter determines how operations are applied: Axis 0 (rows) − Operations are applied along rows, working down vertically. Results are computed across rows for each column. Axis 1 (columns) − Operations are applied along columns, working across horizontally. Results are computed across columns for each row. ...
Read MoreHow to use a List as a dictionary key in Python 3?
Dictionaries are among the most powerful data structures in Python. They consist of key-value pairs and offer O(1) time complexity for accessing values. However, you cannot directly use lists as dictionary keys because lists are mutable and therefore not hashable. Why Lists Cannot Be Dictionary Keys Lists are mutable data types in Python, meaning their contents can be modified after creation. Dictionary keys must be hashable (immutable) because Python uses hash values to efficiently store and retrieve key-value pairs. If a list's contents change after being used as a key, its hash value would change, making it impossible ...
Read MoreHow to Get the next key in Dictionary in Python?
Dictionaries are powerful data types in Python that consist of key-value pairs. While accessing values in a dictionary is straightforward, there may be situations where you need to find the next key in a dictionary. Since Python dictionaries maintain insertion order (Python 3.7+), we can explore different methods to get the next key in a dictionary. Using keys() and index() Method The most straightforward approach is to convert dictionary keys into a list and use indexing to find the next key. Syntax dictionary_name.keys() list_object.index(key_name, start, end) The keys() method returns a view object ...
Read MoreHow to Get a sorted list of random integers with unique elements using Python?
Generating random numbers is one of the most popular techniques in programming, statistics, machine learning models, etc. Creating a sorted list of random integers with unique elements is a common task. In this article, we will explore different approaches to get a sorted list of random integers with unique elements using Python. Using sample() Function from Random Module The random.sample() method generates random samples of k elements from a given population without replacement, ensuring unique elements. Syntax random.sample(population, k) sorted(iterable, key=None, reverse=False) The sample() function takes a population (iterable) and returns k unique ...
Read MoreHow to generate k random dates between two other dates using Python?
Generating random dates is essential in data science applications like neural network training, stock market analysis, and statistical modeling. Python provides several approaches to generate k random dates between two specified dates using different libraries and techniques. Using random and datetime Modules The datetime module handles date operations while random generates random numbers. Combining these modules allows us to create random dates within a specified range. Example The following function generates k random dates by adding random days to the start date within the valid range ? import random from datetime import timedelta, datetime ...
Read MoreGet the Most recent previous business day using Python
In business applications, developers often need to calculate the most recent business day (excluding weekends). This is crucial for financial systems, reporting tools, and data analysis where operations only occur on weekdays. Using the datetime Library The datetime library provides built-in classes to handle dates and times. We can use timedelta to subtract days and check if the result is a business day using the weekday() method. Syntax timedelta(days=) The timedelta class represents the difference between two dates. The days parameter specifies how many days to add or subtract. Example This ...
Read MoreGaussian fit using Python
Data analysis and visualization are crucial nowadays, where data is the new oil. Typically data analysis involves feeding the data into mathematical models and extracting useful information. The Gaussian fit is a powerful mathematical model that data scientists use to model data based on a bell-shaped curve. In this article, we will understand Gaussian fit and how to implement it using Python. What is Gaussian Fit A bell-shaped curve characterizes the Gaussian distribution. The bell-shaped curve is symmetrical around the mean (μ). We define a probability density function as follows: f(x) = (1 / (σ * ...
Read MorePython - Updating value list in dictionary
In Python, dictionaries store data as key-value pairs where values can be lists. Updating these value lists is a common operation when working with dynamic data structures. Syntax dictionary_name[key] = new_value_list You can update a value list by assigning a new list to the specific key using the assignment operator (=). This replaces the existing list associated with that key. Method 1: Direct Assignment Replace the entire value list with a new list ? # Create a dictionary with initial values student_grades = { 'math': [85, 90, ...
Read MoreVisualizing O(n) using Python
Time complexity is a fundamental concept in computer science that measures how an algorithm's runtime changes as the input size grows. O(n) represents linear time complexity, where execution time increases proportionally with input size. Understanding and visualizing O(n) helps developers write efficient code and analyze algorithm performance. What is O(n) Time Complexity? O(n) indicates that an algorithm's execution time grows linearly with the input size 'n'. If you double the input, the execution time roughly doubles too. The most common example is a simple loop that processes each element once ? def linear_search(arr, target): ...
Read MorePython - Visualize graphs generated in NetworkX using Matplotlib
NetworkX is a powerful Python library for creating, manipulating, and studying complex networks. When combined with Matplotlib, it provides excellent capabilities for visualizing graphs with customizable layouts, colors, and labels. Basic Graph Visualization The simplest way to visualize a NetworkX graph is using the draw() function with Matplotlib ? import networkx as nx import matplotlib.pyplot as plt # Create a simple graph G = nx.Graph() G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(3, 1) # Draw and display the graph nx.draw(G, with_labels=True, node_color='lightblue', node_size=500) plt.title("Basic Graph Visualization") plt.show() Visualizing Graphs with Node Labels and Edge ...
Read More