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 38 of 2109
How to Convert dictionary to K sized dictionaries using Python?
Dictionaries are key-value data structures in Python where the keys are unique and values can be repeated. In this article, we will explore how to convert a dictionary into K smaller dictionaries by distributing key-value pairs evenly across K separate dictionaries. Problem Example Given a dictionary with 9 key-value pairs: original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'x': 8, 'y': 9} k = 3 # Expected output: 3 dictionaries with distributed pairs # [{'a': 1, 'd': 4, 'g': 7}, {'b': 2, 'e': 5, 'x': 8}, {'c': ...
Read MoreHow to take integer input in Python?
Taking integer input is a fundamental task in Python programming. The input() function returns strings by default, so we need to convert them to integers using int(). This article explores four common approaches to handle integer input effectively. Using input() and int() Conversion The input() function captures user input as a string. To work with integers, we convert the string using int() function ? Single Integer Input # Taking single integer input num = int(input("Enter an integer: ")) print("You entered:", num) print("Type:", type(num)) Enter an integer: 42 You entered: 42 Type: ...
Read MoreHow to suppress the use of scientific notations for small numbers using NumPy?
When working with NumPy arrays, small numbers are often displayed in scientific notation (like 1e-10). While compact, this format can be difficult to read and compare. This guide covers four methods to suppress scientific notation for small numbers in NumPy arrays: using numpy.vectorize with string formatting, numpy.ndarray.round, string formatting with list comprehension, and numpy.set_printoptions. Method 1: Using numpy.vectorize with String Formatting The numpy.vectorize function combined with string formatting can suppress scientific notation by converting array elements to formatted strings ? Syntax formatted_array = numpy.vectorize('{:.Nf}'.format)(array) Here, N denotes the decimal places to retain, and ...
Read MoreHow to Subtract Two Columns in Pandas DataFrame?
When working with Pandas DataFrames, you often need to perform arithmetic operations between columns. One common operation is subtracting two columns. This guide explores four different methods to subtract columns in a Pandas DataFrame, from simple arithmetic operators to specialized Pandas functions. Method 1: Using Simple Arithmetic Operator (-) The most straightforward approach is using the standard subtraction operator. This method is intuitive and commonly used for basic column operations. Syntax result = dataframe['column1'] - dataframe['column2'] Example Let's create a DataFrame with sales and costs data and calculate the profit by subtracting ...
Read MoreHow to store XML data into a MySQL database using Python?
XML (eXtensible Markup Language) is a widely used format for storing structured data. When working with large XML datasets, storing this data in a MySQL database provides better performance and query capabilities. Python offers excellent libraries to parse XML and interact with MySQL databases. Prerequisites Before starting, ensure you have the required libraries installed ? pip install mysql-connector-python Step 1: Import Required Libraries We need two essential libraries for this task ? xml.etree.ElementTree: For parsing and manipulating XML documents mysql.connector: For connecting Python to MySQL ...
Read MoreHow to Stack Multiple Pandas DataFrames?
Pandas provides several methods to stack multiple DataFrames vertically or horizontally. When working with multiple datasets that need to be combined for analysis, functions like concat(), append() (deprecated), and numpy.vstack() offer different approaches for DataFrame stacking. This article explores the most effective methods for combining multiple DataFrames, focusing on practical examples and their use cases. Syntax pd.concat() pd.concat(objs, axis=0, join='outer', keys=None, ignore_index=False) Concatenates DataFrames along a specified axis with options for join types and index handling. numpy.vstack() numpy.vstack(tup) Stacks arrays vertically (row-wise), provided they have the same number ...
Read MoreHow to split the Dataset With scikit-learnís train_test_split() Function
Machine learning models require proper data splitting to evaluate performance accurately. Scikit-learn's train_test_split() function provides a simple way to divide your dataset into training and testing portions, ensuring your model can be validated on unseen data. Syntax from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) Parameters X, y: Feature matrix and target vector respectively test_size: Proportion of data for testing (typically 0.2 or 20%) random_state: Seed for reproducible random splitting stratify: Maintains class proportions in splits ...
Read MoreHow to Split Data into Training and Testing in Python without Sklearn
Splitting data into training and testing sets is a fundamental step in machine learning. While scikit-learn's train_test_split() is commonly used, understanding how to split data manually helps you grasp the underlying concepts and provides flexibility when external libraries aren't available. Why Split Data? Machine learning models learn patterns from training data. To evaluate how well they generalize to new, unseen data, we need a separate testing set. Using the same data for both training and testing leads to overfitting — the model memorizes the training data but fails on new data. The typical split ratios are 80-20 ...
Read MorePython program to Count Uppercase, Lowercase, special character and numeric values using Regex
Regular expressions, commonly known as re or Regex is a powerful tool for manipulating and searching for patterns in text. In Python, regular expressions are implemented using the re module. A regular expression is a sequence of characters that define a search pattern used to match and manipulate text strings for tasks such as data cleaning, parsing, and validation. To count the number of uppercase letters, lowercase letters, special characters, and numeric values in a string using regular expressions, we use specific patterns to match and count the desired characters. Regex Patterns for Character Counting Uppercase ...
Read MorePython program to display half diamond pattern of numbers with star border
A half-diamond pattern is a geometric pattern that resembles the shape of a diamond, but only covers half of the diamond. Diamond patterns can be created using loops in programming. By controlling the loops and the number of characters printed in each row, we can modify the pattern to achieve different shapes and arrangements. In this article, we will write a Python program that displays a half-diamond pattern of numbers with a star border. Input Output Scenarios Let's explore some input-output scenarios for displaying the half-diamond pattern of numbers with a star border. Scenario 1 ...
Read More