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 312 of 2109
How to append a list to a Pandas DataFrame using iloc in Python?
The iloc method in Pandas provides integer-location based indexing for selecting and modifying DataFrame rows by position. While iloc is primarily used for selection, it can also be used to replace existing rows with new data from a list. Creating a Sample DataFrame Let's start by creating a DataFrame with team ranking data ? import pandas as pd # Data in the form of list of team rankings team_data = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', ...
Read MorePython - Add a new column with constant value to Pandas DataFrame
To add a new column with a constant value to a Pandas DataFrame, use the square bracket notation (index operator) and assign the desired value. This operation broadcasts the constant value across all rows in the DataFrame. Syntax dataframe['new_column_name'] = constant_value Creating a Sample DataFrame First, let's create a DataFrame with sample car data − import pandas as pd # Creating a DataFrame with car information dataFrame = pd.DataFrame({ "Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], ...
Read MorePython - Check if Pandas dataframe contains infinity
A Pandas DataFrame may contain infinity values (inf) from mathematical operations like division by zero. You can check for these values using NumPy's isinf() method and count them with sum(). Detecting Infinity Values First, let's create a DataFrame with some infinity values ? import pandas as pd import numpy as np # Create a dictionary with infinity values d = {"Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000]} # Create DataFrame dataFrame = pd.DataFrame(d) print("DataFrame...") print(dataFrame) DataFrame... Reg_Price 0 7000.505700 1 ...
Read MoreCreate a Pivot Table with multiple columns – Python Pandas
A pivot table is a data summarization tool that reorganizes and aggregates data. In Pandas, you can create pivot tables with multiple columns using the pandas.pivot_table() function to create a spreadsheet-style pivot table as a DataFrame. Syntax pandas.pivot_table(data, index=None, columns=None, values=None, aggfunc='mean') Creating a DataFrame Let's start by creating a DataFrame with team records ? import pandas as pd # Create DataFrame with Team records dataFrame = pd.DataFrame({ 'Team ID': {0: 5, 1: 9, 2: 6, 3: 11, 4: 2, 5: 7}, 'Team ...
Read MorePython - Calculate the minimum of column values of a Pandas DataFrame
To find the minimum value in a Pandas DataFrame column, use the min() function. This method works on individual columns or across the entire DataFrame. Basic Syntax The basic syntax for finding minimum values is ? # For a single column df['column_name'].min() # For all numeric columns df.min() Example 1: Finding Minimum in a Single Column Let's create a DataFrame and find the minimum value in the "Units" column ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', ...
Read MoreHow to exponentially scale the Y axis with matplotlib?
To exponentially scale the Y-axis with matplotlib, you can use the yscale() function with different scale types. The most common approach is using symlog (symmetric logarithmic) scaling, which handles both positive and negative values effectively. Basic Exponential Y-axis Scaling Here's how to create a plot with exponential Y-axis scaling ? import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points dt = 0.01 x = np.arange(-50.0, 50.0, dt) y = np.arange(0, 100.0, dt) # Plot the data plt.plot(x, y) ...
Read MorePython - Convert List of lists to List of Sets
Converting a list of lists to a list of sets is useful for removing duplicates from each inner list while maintaining the outer structure. Python provides several approaches using map(), list comprehensions, and loops. Using map() Function The map() function applies the set() constructor to each inner list ? my_list = [[2, 2, 2, 2], [1, 2, 1], [1, 2, 3], [1, 1], [0]] print("The list of lists is:") print(my_list) my_result = list(map(set, my_list)) print("The resultant list is:") print(my_result) The list of lists is: [[2, 2, 2, 2], [1, 2, ...
Read MorePython program to get all subsets having sum s
When working with lists, you might need to find all subsets that sum to a specific value. Python's itertools.combinations provides an efficient way to generate all possible subsets and check their sums. Using itertools.combinations The combinations function generates all possible subsets of different sizes from the input list ? from itertools import combinations def find_subsets_with_sum(arr, target_sum): result = [] # Generate subsets of all possible sizes (0 to len(arr)) for size in range(len(arr) + 1): ...
Read MorePython - Filter Rows Based on Column Values with query function in Pandas?
To filter rows based on column values, we can use the query() function. In the function, set the condition through which you want to filter records. At first, import the required library − import pandas as pd Following is our data with Team Records − team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4, 65], ['South Africa', 5, 50], ['Bangladesh', 6, 40]] Create a DataFrame from above and add columns as well − dataFrame = pd.DataFrame(team, ...
Read MorePython - Find all the strings that are substrings to the given list of strings
Finding strings that are substrings of other strings in a list is a common task in Python. We can use different approaches including list comprehensions with in operator and the any() function. Using List Comprehension with in Operator This approach checks if each string from one list appears as a substring in any string from another list ? main_strings = ["Hello", "there", "how", "are", "you"] search_strings = ["Hi", "there", "how", "have", "you", "been"] print("Main strings:", main_strings) print("Search strings:", search_strings) # Find strings from search_strings that are substrings of any string in main_strings result = ...
Read More