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
Programming Articles
Page 919 of 2547
Add a row at top in pandas DataFrame
Adding a row at the top of a Pandas DataFrame is a common operation when you need to insert headers, summary rows, or new data at the beginning. There are several methods to achieve this − using pd.concat(), loc[] with index manipulation, or iloc slicing. Create a Sample DataFrame import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['Delhi', 'Mumbai', 'Pune'] }) print("Original DataFrame:") print(df) Original DataFrame: Name ...
Read MoreAbsolute and Relative frequency in Pandas
In statistics, frequency indicates how many times a value appears in a dataset. Absolute frequency is the raw count, while relative frequency is the proportion (count divided by total observations). Pandas provides built-in methods for calculating both. Absolute Frequency Using value_counts() The simplest way to count occurrences of each value ? import pandas as pd data = ["Chandigarh", "Hyderabad", "Pune", "Pune", "Chandigarh", "Pune"] df = pd.Series(data).value_counts() print(df) Pune 3 Chandigarh 2 Hyderabad 1 dtype: int64 ...
Read MoreAbsolute Deviation and Absolute Mean Deviation using NumPy
In statistics, data variability measures how dispersed values are in a sample. Two key measures are Absolute Deviation (difference of each value from the mean) and Mean Absolute Deviation (MAD) (average of all absolute deviations). NumPy provides efficient functions to calculate both. Formulas $$\mathrm{Absolute\:Deviation_i = |x_i - \bar{x}|}$$ $$\mathrm{MAD = \frac{1}{n}\sum_{i=1}^{n}|x_i - \bar{x}|}$$ Where xi is each data point, x̄ is the mean, and n is the sample size. Absolute Deviation Calculate the absolute deviation for each element in a data sample ? from numpy import mean, absolute data = [12, ...
Read MoreA += B Assignment Riddle in Python
The += operator on a mutable object inside a tuple creates a surprising Python riddle − the operation succeeds (the list gets modified) but also raises a TypeError because tuples don't support item assignment. Both things happen simultaneously. The Riddle Define a tuple with a list as one of its elements, then try to extend the list using += ? tupl = (5, 7, 9, [1, 4]) tupl[3] += [6, 8] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment ...
Read MoreAdd new column in Pandas Data Frame Using a Dictionary
A Pandas DataFrame is a two-dimensional tabular data structure with rows and columns. You can add a new column by mapping values from a Python dictionary to an existing column using the map() function. Creating a DataFrame First, create a DataFrame from a Pandas Series ? import pandas as pd s = pd.Series([6, 8, 3, 1, 12]) df = pd.DataFrame(s, columns=['Month_No']) print(df) Month_No 0 6 1 8 2 ...
Read MoreAccessing elements of a Pandas Series
A Pandas Series is a one-dimensional labeled array that can hold any data type. Elements can be accessed using integer position, custom index labels, or slicing. Creating a Series import pandas as pd s = pd.Series([11, 8, 6, 14, 25], index=['a', 'b', 'c', 'd', 'e']) print(s) a 11 b 8 c 6 d 14 e 25 dtype: int64 Accessing a Single Element Use integer position or custom label to access individual elements ? ...
Read Morea.sort, sorted(a), np_argsort(a) and np.lexsort(b, a) in Python
Python provides built-in sorting functions (sorted(), list.sort()) and NumPy provides advanced sorting (np.argsort(), np.lexsort()) for working with arrays and multiple sort keys. sorted() Returns a new sorted list without modifying the original ? a = [9, 5, 3, 1, 12, 6] b = sorted(a) print("Sorted Array:", b) print("Original Array:", a) Sorted Array: [1, 3, 5, 6, 9, 12] Original Array: [9, 5, 3, 1, 12, 6] list.sort() Sorts the list in-place (modifies the original, returns None). Faster than sorted() since it doesn't create a copy ? a = ...
Read MoreAbsolute and Relative Imports in Python
In Python, when you need to access code from another file or package, you use import statements. There are two approaches − absolute imports (full path from the project root) and relative imports (path relative to the current file). How Python Resolves Imports When Python encounters an import statement, it searches in this order − Module cache − Checks sys.modules for previously imported modules. Built-in modules − Searches Python's standard library. sys.path − Searches directories in sys.path (current directory first). If not found anywhere, raises ModuleNotFoundError. Import Order Convention Import statements should be ...
Read MoreMaximum count of characters that can replace ? by at most A 0s and B 1s with no adjacent duplicates
In this problem, we need to replace '?' characters in a string with '0's and '1's such that no two adjacent characters are the same, while maximizing the number of replacements using at most A zeros and B ones. Syntax int maximumChar(char *str, int aCount, int bCount); Problem Statement Given a string containing '*' and '?' characters, and two integers A and B representing available 0s and 1s, find the maximum number of '?' characters that can be replaced without creating adjacent duplicates. Algorithm The approach involves the following steps − ...
Read MorePosition of the leftmost set bit in a given binary string where all 1s appear at the end
The aim of this article is to implement a program to find the position of the leftmost set bit in a given binary string where all 1s appear at the end. A binary string is a sequence of bits (0s and 1s) used to represent data in binary format. In this problem, we are given a binary string where all the 1s are grouped together at the rightmost positions, and we need to find the leftmost position where the first '1' appears. Syntax int findFirstBit(char* str, int length); Problem Statement Given a binary ...
Read More