Articles on Trending Technologies

Technical articles with clear explanations and examples

Write a program in Python to perform flatten the records in a given dataframe by C and F order

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 246 Views

When working with DataFrames, you may need to flatten the data into a one-dimensional array. Python Pandas provides the ravel() function which can flatten data in different orders: C order (row-major) and F order (column-major). Understanding C and F Order The order parameter determines how multi-dimensional data is flattened ? C order (row-major): Flattens row by row, reading elements from left to right F order (column-major): Flattens column by column, reading elements from top to bottom Creating the DataFrame Let's start by creating a sample DataFrame with ID and Age columns ? ...

Read More

Write a program in Python to print dataframe rows as orderDict with a list of tuple values

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 196 Views

In Pandas, you can convert DataFrame rows to OrderedDict objects with list of tuple values. This is useful when you need to maintain the order of columns and access row data in a structured dictionary format. Understanding the Problem When working with DataFrames, sometimes you need each row as an OrderedDict where each column-value pair is represented as a tuple. The expected output format is ? OrderedDict([('Index', 0), ('Name', 'Raj'), ('Age', 13), ('City', 'Chennai'), ('Mark', 80)]) OrderedDict([('Index', 1), ('Name', 'Ravi'), ('Age', 12), ('City', 'Delhi'), ('Mark', 90)]) OrderedDict([('Index', 2), ('Name', 'Ram'), ('Age', 13), ('City', 'Chennai'), ('Mark', 95)]) ...

Read More

Write a program in Python to caluculate the adjusted and non-adjusted EWM in a given dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 279 Views

The Exponentially Weighted Moving Average (EWM) is a statistical technique that gives more weight to recent observations. Pandas provides two modes: adjusted (default) and non-adjusted, which handle the calculation differently during the initial periods. Understanding EWM Parameters The key difference between adjusted and non-adjusted EWM lies in how they handle the bias correction ? Adjusted EWM (default): Applies bias correction to account for the initialization period Non-adjusted EWM: Uses raw exponential weighting without bias correction com parameter: Center of mass, controls the decay rate (higher values = slower decay) Creating Sample Data First, ...

Read More

Write a Python code to fill all the missing values in a given dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 370 Views

When working with datasets, missing values (NaN) are common. Pandas provides the interpolate() method to fill missing values using various interpolation techniques like linear, polynomial, or time-based methods. Syntax df.interpolate(method='linear', limit_direction='forward', limit=None) Parameters method − Interpolation technique ('linear', 'polynomial', 'spline', etc.) limit_direction − Direction to fill ('forward', 'backward', 'both') limit − Maximum number of consecutive NaNs to fill Example Let's create a DataFrame with missing values and apply linear interpolation ? import pandas as pd df = pd.DataFrame({"Id": [1, 2, 3, None, 5], ...

Read More

Write a Python code to rename the given axis in a dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 358 Views

In Pandas, you can rename the axis (row index or column names) of a DataFrame using the rename_axis() method. This is useful when you want to give a meaningful name to your DataFrame's index or columns axis. Syntax DataFrame.rename_axis(mapper, axis=None, copy=None, inplace=False) Parameters mapper − The new name for the axis axis − 0 or 'index' for row axis, 1 or 'columns' for column axis inplace − If True, modify the DataFrame in place Renaming the Column Axis Let's create a DataFrame and rename its column axis ? ...

Read More

Write a Python code to find a cross tabulation of two dataframes

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 613 Views

Cross-tabulation (crosstab) creates a frequency table showing relationships between categorical variables from different DataFrames. Pandas provides the pd.crosstab() function to compute cross-tabulations between two or more factors. Creating Sample DataFrames Let's start by creating two DataFrames with related data ? import pandas as pd # First DataFrame with Id and Age df = pd.DataFrame({'Id': [1, 2, 3, 4, 5], 'Age': [12, 13, 12, 13, 14]}) print("DataFrame 1:") print(df) # Second DataFrame with Mark df1 = pd.DataFrame({'Mark': [80, 90, 80, 90, 85]}) print("DataFrame 2:") print(df1) DataFrame 1: Id ...

Read More

Write a program in Python to print the length of elements in all column in a dataframe using applymap

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 460 Views

The applymap() function in Pandas allows you to apply a function element-wise to every cell in a DataFrame. This is useful when you want to calculate the length of string elements across all columns. Understanding applymap() The applymap() method applies a function to each element of the DataFrame. Unlike apply(), which works on rows or columns, applymap() works on individual elements. Syntax DataFrame.applymap(func) Where func is the function to apply to each element. Example Let's create a DataFrame and calculate the length of elements in all columns ? import ...

Read More

Write a Python code to calculate percentage change between Id and Age columns of the top 2 and bottom 2 values

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 503 Views

Sometimes we need to calculate percentage changes between consecutive rows in specific columns of a DataFrame. The pct_change() method calculates the percentage change from the previous row, which is useful for analyzing trends in data. Understanding Percentage Change The pct_change() method computes the percentage change between the current and previous element. The formula is: (current - previous) / previous. Example Dataset Let's start by creating a sample DataFrame with Id and Age columns ? import pandas as pd df = pd.DataFrame({ "Id": [1, 2, 3, None, 5], ...

Read More

Write a Python program to perform table-wise pipe function in a dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 286 Views

The pipe() function in Pandas allows you to apply a custom function to an entire DataFrame. This is useful for performing table-wise operations where you want to transform the entire dataset using a user-defined function. Understanding DataFrame pipe() Function The pipe() method passes the DataFrame as the first argument to a function, along with any additional arguments you specify. This enables method chaining and cleaner code organization. Syntax DataFrame.pipe(func, *args, **kwargs) Example: Table-wise Operation Let's create a DataFrame and apply a custom function using pipe() ? import pandas as pd ...

Read More

Write a Python program to trim the minimum and maximum threshold value in a dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 578 Views

Sometimes you need to limit values in a DataFrame to fall within specific minimum and maximum thresholds. Pandas provides the clip() method to trim values that exceed these boundaries. Understanding DataFrame Clipping The clip() method constrains values between a lower and upper limit: lower parameter sets the minimum threshold upper parameter sets the maximum threshold Values below the lower limit are replaced with the lower limit Values above the upper limit are replaced with the upper limit Syntax DataFrame.clip(lower=None, upper=None, axis=None) Creating Sample Data Let's create a DataFrame with ...

Read More
Showing 5411–5420 of 61,299 articles
« Prev 1 540 541 542 543 544 6130 Next »
Advertisements