How to drop the last rows in Pandas DataFrames?

In this tutorial we’ll learn how to remove the one or several last rows of a DataFrame. We’ll be touching on several cases: Getting the last (or last n) rows in a DataFrame. Removing the last (or last n) rows from the DataFrame. Dropping all rows except the first row Drop the last column Example … Read more

How to convert strings to datetime objects in Python?

In today’s quick tutorial we’ll learn how to easily transform string objects to datetime objects. We’ll look into three main topics: Transform a simple string to datetimes. Convert a list of strings to a list of datetime objects Troubleshooting the module not found strptime error Strings to date times in Python 3 The function datetime.datetime … Read more

How to read files into a string in Python 3?

In this short Python tutorial we’ll learn how to load the contents one or multiple files (being both text, csv) to a string variable object with Python 3. We will cover several scenarios: Load the content of a single text file. Reading multiple text files in a directory. Reading a csv file. Append the read … Read more

How to solve the no module named error in Python?

Often times, when developing with Python, we encounter errors related to third party module not found in our development environment. These are generally known as “no module named errors”. In this tutorial we would like to answer several questions related to the topic, so you can easily tackle those issues when trying to import and … Read more

How to sum days and time to a date in Python?

Today we will learn how to quickly add different time periods to date objects using the Python programming language. Add days to a date object in Python In this first example, we will assume that we would like to calculate the expected start date of a new hire in our fictitious company. Let us start … Read more

Fix the typerror can’t multiply sequence by float in Python

In today’s short tutorial we’ll help you troubleshoot a type error that you might receive when trying to multiply a sequence (strings, lists or tuples) by a float number in Python. Can’t multiple sequence by float error Let’s run the following code: After entering the product price, as expected, we’ll get the following error: Fixing … Read more

How to subtract dates and times in Python?

In today’s Python automation tutorial we would like to show how you can easily subtract and calculate time differences using the Python language. We’ll use the datetime module to calculate: Difference between two dates Difference between a date and a timedelta object. Subtracting other timedeltas from date objects : years, months, hours, minutes, seconds. Subtract … Read more

Write to a CSV file row by row with Python

This tutorial explains how to use Python 3 to write data line by line into a comma separated value file. We’ll look into several examples: Using the csv module. Using the Pandas Data Analysis library Write text using the CSV module In the first example we’ll use the Python csv library to define a writer … Read more