Server Side Programming Articles

Page 433 of 2109

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

Write a program in Python to resample a given time series data and find the maximum month-end frequency

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

Time series resampling in Pandas allows you to change the frequency of your data and apply aggregation functions. In this tutorial, we'll learn how to resample time series data to find the maximum month-end frequency using the resample() method. Understanding the Problem We have a time series dataset with weekly data points and want to group them by month, then find the maximum values for each month. The result will show month-end dates with the corresponding maximum values. Step-by-Step Solution Step 1: Create the DataFrame First, we'll create a DataFrame with ID numbers and generate ...

Read More

Write a Python program to read an Excel data from file and read all rows of first and last columns

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 1K+ Views

Reading specific columns from an Excel file is a common data analysis task. Python's pandas library provides the iloc method to select rows and columns by their position, making it easy to extract the first and last columns from any dataset. Understanding Column Selection with iloc The iloc method uses integer-based indexing where: df.iloc[:, 0] selects all rows of the first column (index 0) df.iloc[:, -1] selects all rows of the last column (index -1) The colon (:) means "all rows" Creating Sample Data First, let's create a sample Excel file to demonstrate ...

Read More

Write a program in Python to read CSV data from a file and print the total sum of last two rows

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 2K+ Views

When working with CSV files in Python, you often need to perform calculations on specific rows. This tutorial shows three different methods to read CSV data and calculate the sum of the last two rows using pandas. Sample CSV Data First, let's create a sample CSV file named pandas.csv: Id, Data 1, 11 2, 22 3, 33 4, 44 5, 55 6, 66 7, 77 8, 88 9, 99 10, 100 Using tail() Method The tail() method returns the last n rows of a DataFrame. Combined with sum(), it provides a clean solution ...

Read More
Showing 4321–4330 of 21,090 articles
« Prev 1 431 432 433 434 435 2109 Next »
Advertisements