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

Replace Values in Pandas DataFrame

Here are 4 ways to replace values in Pandas DataFrame: (1) Replace a single value with a new value: Copy df[“column_name”] = df[“column_name”].replace([“old_value”], “new_value”) (2) Replace multiple values with a new value: Copy df[“column_name”] = df[“column_name”].replace([“1st_old_value”, “2nd_old_value”, …], “new_value”) (3) Replace multiple values with multiple new values: Copy df[“column_name”] = df[“column_name”].replace([“1st_old_value”, “2nd_old_value”, …], [“1st_new_value”, “2nd_new_value”, … Read more

Measure the Time it Takes to Run a Python Script

To measure the time it takes to run a Python script: Copy import timestart_time = time.time()# Place your python script hereexecution_time = time.time() – start_timeprint(“Execution time in seconds: ” + str(execution_time)) Steps to Measure the Time it Takes to Run a Python Script Step 1: Write the Python Script For illustration purposes, let’s write a Python … Read more

How to Change the Pandas Version in Windows

You can change the Pandas version in Windows using this command: Copy pip install pandas==pandas version needed –user For example, if the Pandas version needed is 1.1.1, then open the Windows Command Prompt and type/copy the command below: Copy pip install pandas==1.1.1 –user Note that the above method would work only if you added Python … Read more

3 Ways to Create NaN Values in Pandas DataFrame

In this article, you’ll see 3 ways to create NaN values in Pandas DataFrame: 3 Ways to Create NaN Values in Pandas DataFrame (1) Using Numpy You can easily create NaN values in Pandas DataFrame using Numpy. More specifically, you can place np.nan each time you want to add a NaN value in the DataFrame. … Read more