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

How to Run One Python Script From Another

In this short guide, you’ll see how to run one Python script from another Python script. Steps Step 1: Place the Python Scripts in the Same Folder To start, place your Python scripts in the same folder. For example, assume that two Python scripts (called python_1 and python_2) are stored in the same folder: python_1python_2 … Read more

Plot a Line Chart in Python using Matplotlib

In this short guide, you’ll see how to plot a Line chart in Python using Matplotlib. To start, here is a template that you may use to plot your Line chart: Copy import matplotlib.pyplot as pltx_axis = [“value_1”, “value_2”, “value_3”, …]y_axis = [“value_1”, “value_2”, “value_3”, …]plt.plot(x_axis, y_axis)plt.title(“title name”)plt.xlabel(“x_axis name”)plt.ylabel(“y_axis name”)plt.show() Next, you’ll see how to … Read more

How to Convert Strings to Datetime in Pandas DataFrame

You may use this template in order to convert strings to datetime in Pandas DataFrame: Copy df[‘DataFrame Column’] = pd.to_datetime(df[‘DataFrame Column’], format=specify your format) Note that the strings must match the format specified. Later, you’ll see several scenarios for different formats. Steps to Convert Strings to Datetime in Pandas DataFrame Step 1: Collect the Data … Read more