How Does Pandas Concat Work?

The pandas.concat( ) function combines the data from multiple Series and/or DataFrames fast and in an intuitive manner. It is one of the most basic data wrangling operations used in Pandas. In general, we draw some conclusions from the data by analyzing it. The confidence in our conclusions increases as we include more variables or … Read more

np.shape()

This tutorial explains NumPy’s shape() function. Return the shape of an array or array_like object a. Argument Data Type Description a array_like NumPy array or Python list for which the shape should be returned. If it is a NumPy array, it returns the attribute a.shape. If it is a Python list, it returns a tuple … Read more

How to Calculate the Column Variance of a DataFrame in Python Pandas?

Want to calculate the variance of a column in your Pandas DataFrame? In case you’ve attended your last statistics course a few years ago, let’s quickly recap the definition of variance: it’s the average squared deviation of the list elements from the average value. You can calculate the variance of a Pandas DataFrame by using … Read more

Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug)

Do you encounter this stupid error? You’re not alone—thousands of coders like you generate this error in thousands of projects every single month. This short tutorial will show you exactly why this error occurs, how to fix it, and how to never make the same mistake again. So, let’s get started! Python throws the TypeError … Read more

How to Convert a Float List to an Integer List in Python

The most Pythonic way to convert a list of floats fs to a list of integers is to use the one-liner fs = [int(x) for x in fs]. It iterates over all elements in the list fs using list comprehension and converts each list element x to an integer value using the int(x) constructor. This … Read more

10 Minutes to Pandas (in 5 Minutes)

This tutorial provides you a quick and dirty introduction to the most important Pandas features. A popular quickstart to the Pandas library is provided by the official “10 Minutes to Pandas” guide. This tutorial in front of you aims to cover the most important 80% of the official guide, but in 50% of the time. … Read more

Python Metaclasses

Summary: Metaclasses are a class of a class. In other words, a class is an instance of a metaclass. Metaclass can be created using two methods: (1) type Class which is the built-in metaclass in Python. (2) Creating custom metaclass using the metaclass keyword. Problem: What are metaclasses and why do we use them in … Read more

How To Eliminate All The Whitespace From A String?

In this article, you’ll learn the ultimate answer to the following question: How To Eliminate All The Whitespace From A String—on Both Ends, and In-Between Words? Summary: Use the string methods join(), split(), strip(), rstrip(), lstrip() and or replace()—in specific combinations—to remove any whitespace in a given string. The simplest way to remove all whitespaces … Read more

The Mutable Default Argument In Python

Summary: Passing mutable objects as default arguments leads to unexpected outputs because Python initializes the default mutable object only once, not (as you may have expected) each time the function is called. To fix this, initialize the mutable default argument with the None keyword in the argument list and then initialize it within the function. … Read more

Slice Notation – A Simple Illustrated Guide

Summary: Slicing is a Python concept to extract a subsequence from a string or list—that lies within a start and stop index range. There are two syntactical ways to define a slice. (1) The extended slice notation makes use of a colon : in string_name[start:stop:step]. (2) The slice() constructor defines the index range in string_name[slice(start:stop:step)]. … Read more