Select Rows Containing a Substring in Pandas DataFrame

In this short guide, you’ll see how to select rows that contain a specific substring in Pandas DataFrame. In particular, you’ll observe 5 cases to get all rows that: The Example To start, create a DataFrame in Python with the following data: Copy import pandas as pddata = { “month”: [“January”, “February”, “March”, “April”, “May”, … Read more

Drop a Table in SQL Server using Python

In this short guide, you’ll see how to drop a table in SQL Server using Python. Example is also included for illustration purposes. Steps to Drop a Table in SQL Server using Python Step 1: Install the Pyodbc package The Pyodbc package can be used to connect Python to SQL Server. If you haven’t already … Read more

Create a Scatter Diagram in Python using Matplotlib

To create a scatter diagram in Python using Matplotlib: Copy import matplotlib.pyplot as pltx_axis = [“value_1”, “value_2”, “value_3”, …]y_axis = [“value_1”, “value_2”, “value_3”, …]plt.scatter(x_axis, y_axis)plt.title(“title name”)plt.xlabel(“x_axis name”)plt.ylabel(“y_axis name”)plt.show() Steps to Create a Scatter Diagram in Python using Matplotlib Step 1: Install the Matplotlib package If you haven’t already done so, install Matplotlib using the following … Read more

Convert Index to Column in Pandas DataFrame

To convert an index to a column in Pandas DataFrame (with an “index” header): Copy df.reset_index(inplace=True) And if you want to rename the “index” header to a customized header, then use: Copy df.reset_index(inplace=True)df.rename(columns={“index”: “new_column_name”}, inplace=True) Steps to Convert Index to Column in Pandas DataFrame Step 1: Create a DataFrame To begin, create a DataFrame with … Read more

Create a Table in SQL Server using Python

In this article, you’ll see how to create a table in SQL Server using Python. An example is also included for demonstration purposes. Steps to Create a Table in SQL Server using Python Step 1: Install the Pyodbc package If you haven’t already done so, install the Pyodbc package using this command: Copy pip install … Read more

Get a List of all Column Names in Pandas DataFrame

Here are two approaches to get a list of all the column names in Pandas DataFrame: (1) Using list(df) Copy my_list = list(df) (2) Using df.columns.values.tolist() Copy my_list = df.columns.values.tolist() The Example To start with a simple example, let’s create a DataFrame with 3 columns: Copy import pandas as pd data = {‘Name’: [‘Bill’, ‘Maria’, … Read more

Convert Integers to Datetime in Pandas DataFrame

To convert integers to datetime in Pandas DataFrame: Copy df[“DataFrame Column”] = pd.to_datetime(df[“DataFrame Column”], format=specify format) Note that the integers must match the format specified. The Examples Example 1: Date format of “YYYYMMDD“ To start with a simple example, create a DataFrame with a column called “dates” that contains integers (with a format of YYYYMMDD): … Read more

Set Column as Index in Pandas DataFrame

Two approaches to set a column as the index in Pandas DataFrame: (1) Set a single column as Index: Copy df.set_index(‘column’, inplace=True) (2) Set multiple columns as MultiIndex: Copy df.set_index([‘column_1’, ‘column_2′, …], inplace=True) Steps to Set Column as Index in Pandas DataFrame Step 1: Create a DataFrame To start with a simple example, let’s say … Read more

Convert Pandas Series to DataFrame

To convert your Series to a DataFrame: Copy df = ser.to_frame() Alternatively, you can use this approach to convert your Series: Copy df = pd.DataFrame(ser) Steps to Convert Pandas Series to DataFrame Step 1: Create a Series To start with a simple example, let’s create a Pandas Series from a List of 5 items: Copy … Read more

How to Create Pandas Series from a List

You can create Pandas Series from a list using this syntax: Copy pd.Series(list_name) Steps to Create Pandas Series from a List Step 1: Create a List To start, let’s create a list that contains 5 names: Copy people_list = [‘Jon’, ‘Mark’, ‘Maria’, ‘Jill’, ‘Jack’] print(people_list) This is how the list would look like: The ultimate … Read more