Python Articles

Page 154 of 855

How to Count the Number of Rows in a MySQL Table in Python?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 10K+ Views

Counting the number of rows in a MySQL table is a common operation when working with databases. In Python, you can use MySQL connector libraries to establish a connection and execute SQL queries. This article demonstrates two effective approaches: using the cursor's execute() method and the SQL COUNT(*) function. Database Setup For our examples, we'll use a database named insillion with a table called bikes containing the following data: mysql> SELECT * FROM bikes; +----+-------+-------+ | id | name | price | +----+-------+-------+ | 1 | Bajaj | 2543 | | 2 ...

Read More

How to Convert Pandas to PySpark DataFrame?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 6K+ Views

Pandas and PySpark are two popular data processing tools in Python. While Pandas is well-suited for working with small to medium-sized datasets on a single machine, PySpark is designed for distributed processing of large datasets across multiple machines. Converting a pandas DataFrame to a PySpark DataFrame becomes necessary when you need to scale up your data processing to handle larger datasets. This guide explores two main approaches for converting pandas DataFrames to PySpark DataFrames. Syntax The basic syntax for creating a PySpark DataFrame is ? spark.createDataFrame(data, schema) Here, data is the pandas DataFrame ...

Read More

How to Convert HTML to Markdown in Python?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 16K+ Views

Markdown is a lightweight markup language that allows you to write formatted text that can be easily read and understood on the web. Converting HTML to Markdown can be useful when you want to simplify content or make it more readable for documentation, blogs, or text editors. The markdownify package in Python provides a simple and efficient way to convert HTML text to Markdown format. This article demonstrates how to install and use markdownify to convert various HTML structures into clean Markdown text. Installation The markdownify module is not pre-installed with Python, so you need to install ...

Read More

How to convert CSV File to PDF File using Python?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 5K+ Views

In today's data-driven world, being able to convert CSV files to more presentable PDF format is a common requirement. Python provides powerful libraries that make this conversion process straightforward and efficient. This tutorial demonstrates how to convert CSV files to PDF using Python by first converting the CSV to HTML format using pandas, then converting the HTML to PDF using pdfkit. Required Libraries and Setup Before starting, you'll need to install the required libraries and the wkhtmltopdf utility ? pip install pandas pdfkit You also need to install wkhtmltopdf from: https://wkhtmltopdf.org/downloads.html Sample ...

Read More

How to convert CSV columns to text in Python?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 9K+ Views

CSV (Comma Separated Values) files are commonly used to store and exchange tabular data. However, there may be situations where you need to convert the data in CSV columns to text format, for example, to use it as input for natural language processing tasks or data analysis. Python provides several tools and libraries that can help with this task. In this tutorial, we will explore different methods for converting CSV columns to text in Python using the Pandas library. Approach Load the CSV file into a pandas DataFrame using the read_csv() function. Extract the desired column ...

Read More

How to convert categorical data to binary data in Python?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 4K+ Views

Categorical data, also known as nominal data, is a type of data that is divided into discrete categories or groups. These categories have no inherent order or numerical value, and they are usually represented by words, labels, or symbols. Categorical data is commonly used to describe characteristics or attributes of objects, people, or events, and it can be found in various fields such as social sciences, marketing, and medical research. In Python, categorical data can be represented using various data structures, such as lists, tuples, dictionaries, and arrays. The most commonly used data structure for categorical data in Python ...

Read More

How to convert a NumPy array to a dictionary in Python?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 9K+ Views

Converting a NumPy array to a dictionary in Python is useful when you need dictionary operations like key-based lookups or when working with APIs that require dictionary inputs. This tutorial demonstrates multiple approaches to perform this conversion. Understanding NumPy Arrays A NumPy array is a table of elements (typically numbers) of the same data type, indexed by a tuple of positive integers. The ndarray class provides efficient storage and operations for multi-dimensional data. Method 1: Converting Individual Elements to Dictionary This approach flattens the array and creates a dictionary where keys are indices and values are ...

Read More

Formatting containers using format() in Python

Atharva Shah
Atharva Shah
Updated on 27-Mar-2026 338 Views

The format() method in Python provides powerful ways to control how containers like lists, tuples, dictionaries, and sets are displayed. This method allows you to customize alignment, padding, precision, and presentation of your data structures for better readability. Formatting Lists You can format lists by joining elements with custom separators using format() ? my_list = [1, 2, 3, 4, 5] formatted_list = ', '.join(['{}'.format(x) for x in my_list]) print(formatted_list) 1, 2, 3, 4, 5 The join() method combines the formatted numbers with a comma and space separator. Each integer value replaces ...

Read More

Emulating Numeric Types in Python

Atharva Shah
Atharva Shah
Updated on 27-Mar-2026 804 Views

Python includes built-in mathematical data structures like complex numbers, floating-point numbers, and integers. But occasionally we might want to develop our own custom-behaved number classes. Here, the idea of imitating number classes is put into use. We can create objects that can be used in the same way as native numeric classes by simulating them using special methods (also called "magic methods" or "dunder methods"). Basic Addition with __add__ The simplest way to emulate numeric behavior is implementing the __add__ method − class MyNumber: def __init__(self, value): ...

Read More

Difference Between Set vs List vs Tuple

Atharva Shah
Atharva Shah
Updated on 27-Mar-2026 2K+ Views

Python provides three essential data structures for storing collections: lists, tuples, and sets. Each serves different purposes with unique characteristics that make them suitable for specific use cases. List A list is a mutable, ordered collection that allows duplicate elements. Items can be changed, added, or removed after creation ? # Create a list fruits = ['apple', 'banana', 'orange'] print("Original list:", fruits) # Access elements by index print("First fruit:", fruits[0]) # Add elements fruits.append('kiwi') print("After append:", fruits) # Remove elements fruits.remove('banana') print("After removal:", fruits) # Check membership print("Is 'apple' in list?", 'apple' ...

Read More
Showing 1531–1540 of 8,549 articles
« Prev 1 152 153 154 155 156 855 Next »
Advertisements