Pandas Articles

Page 21 of 42

Write a Python program to find the mean absolute deviation of rows and columns in a dataframe

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

Mean Absolute Deviation (MAD) measures the average distance between each data point and the mean of the dataset. In pandas, you can calculate MAD for both rows and columns of a DataFrame using the mad() method. What is Mean Absolute Deviation? MAD is calculated as the mean of absolute deviations from the arithmetic mean: MAD = mean(|x - mean(x)|) Creating a Sample DataFrame Let's start by creating a DataFrame with sample data ? import pandas as pd data = {"Column1": [6, 5.3, 5.9, 7.8, 7.6, 7.45, 7.75], ...

Read More

Write a Python program to find the average of first row in a Panel

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

A Panel was a 3-dimensional data structure in older versions of pandas (deprecated since v0.25). To find the average of the first row, we use the major_xs() method to select a specific row and then calculate its mean. Creating a Panel First, let's create a Panel with sample data ? import pandas as pd import numpy as np # Create data dictionary with DataFrame data = {'Column1': pd.DataFrame(np.random.randn(5, 3))} # Create Panel from data p = pd.Panel(data) print("Panel values:") print(p['Column1']) Panel values: ...

Read More

Write a program in Python to find the minimum rank of a particular column in a dataframe

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

Finding the minimum rank of values in a DataFrame column is useful for data analysis and ranking operations. Pandas provides the rank() method with different ranking strategies including the minimum rank approach. Understanding Minimum Rank Minimum rank assigns the lowest possible rank to tied values. For example, if two values tie for 1st place, both get rank 1, and the next rank becomes 3 (skipping rank 2). Creating the DataFrame Let's start by creating a sample DataFrame with age data ? import pandas as pd data = {'Id': [1, 2, 3, 4, 5], ...

Read More

Write a program in Python to create a panel from a dictionary of dataframe and print the maximum value of the first column

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

Pandas Panel was a 3-dimensional data structure in older versions of pandas. Though deprecated, understanding how to work with multi-dimensional data and extract maximum values from specific axes remains valuable for data analysis. Problem Statement We need to create a panel from a dictionary of DataFrames and find the maximum value in the first column across all items in the panel. Solution Approach To solve this problem, we will follow these steps ? Create a dictionary with DataFrame containing random data Convert the dictionary to a Panel structure Use minor_xs() to select the first ...

Read More

Write a program in Python to shift a dataframe index by two periods in positive and negative direction

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

In pandas, you can shift DataFrame values by a specified number of periods using the shift() method. This is useful for time series analysis, creating lag variables, or comparing data across different time periods. Understanding DataFrame Shifting The shift() method moves data along the specified axis: Positive values shift data down (forward in time) Negative values shift data up (backward in time) Shifted positions are filled with NaN values Syntax DataFrame.shift(periods=1, freq=None, axis=0, fill_value=None) Creating the DataFrame Let's create a time-indexed DataFrame to demonstrate shifting ? import ...

Read More

Write a program in Python to remove first duplicate rows in a given dataframe

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

Duplicate rows in a DataFrame can clutter your data analysis. In pandas, you can remove duplicate rows using the drop_duplicates() method. When you set keep='last', it removes the first occurrence of duplicates and keeps the last one. Understanding the Problem Let's start by creating a DataFrame with duplicate rows to see how duplicate removal works ? import pandas as pd df = pd.DataFrame({ 'Id': [1, 2, 3, 4, 5, 6, 2, 7, 3, 9, 10], 'Age': [12, 13, 14, 13, 14, 12, 13, 16, 14, 15, 14] ...

Read More

Write a program in Python to compute grouped data covariance and calculate grouped data covariance between two columns in a given dataframe

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

Covariance measures how much two variables change together. In pandas, you can compute grouped data covariance using groupby() with cov() to analyze relationships within different groups of your data. Understanding Grouped Covariance When you have categorical data, computing covariance within each group helps identify patterns specific to each category. The cov() function returns a covariance matrix showing relationships between all numeric columns. Creating Sample Data Let's start with a DataFrame containing student marks grouped by subjects ? import pandas as pd df = pd.DataFrame({ 'subjects': ['maths', 'maths', 'maths', 'science', ...

Read More

Write a program to truncate a dataframe time series data based on index value

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

When working with time series data in pandas, you often need to extract a specific date range from your DataFrame. The truncate() method allows you to filter data based on index values, making it useful for time-based filtering. Understanding DataFrame Truncation The truncate() method filters DataFrame rows based on index values using before and after parameters. For time series data, this is particularly useful when your DataFrame has a datetime index. Creating Sample Time Series Data Let's start by creating a DataFrame with time series data ? import pandas as pd # Create ...

Read More

Write a program in Python to compute autocorrelation between series and number of lags

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

Autocorrelation measures the correlation between a time series and its lagged version. In pandas, the autocorr() method computes the Pearson correlation coefficient between a series and its lagged values. Understanding Autocorrelation Autocorrelation helps identify patterns and dependencies in time series data. A lag of 1 compares each value with the previous value, lag of 2 compares with the value two positions back, and so on. Creating a Series Let's create a pandas Series with some sample data including a NaN value ? import pandas as pd import numpy as np series = pd.Series([2, ...

Read More

Write a program in Python to export a given dataframe into Pickle file format and read the content from the Pickle file

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

Pickle is a Python serialization format that preserves the exact data types and structure of pandas DataFrames. This tutorial shows how to export a DataFrame to a pickle file and read it back. What is Pickle Format? Pickle is Python's native binary serialization format that maintains data types, index information, and DataFrame structure perfectly. Unlike CSV, pickle preserves datetime objects, categorical data, and multi-level indexes. Creating and Exporting DataFrame to Pickle Let's create a sample DataFrame and export it to pickle format ? import pandas as pd # Create a sample DataFrame df ...

Read More
Showing 201–210 of 418 articles
« Prev 1 19 20 21 22 23 42 Next »
Advertisements