5 Examples Using Dict Comprehension in Python

List Comprehension is really a handy and faster way to write for loops in Python in just a single line of code. The idea of comprehension is not just unique to lists in Python. Dictionaries, one of the commonly used data structures in data science, can also do comprehension. This is called dict comprehension or […]

How to Load a Massive File as small chunks in Pandas?

The longer you work in data science, the higher the chance that you might have to work with a really big file with thousands or millions of lines. Trying to load all the data at once in memory will not work as you will end up using all of your RAM and crash your computer. […]

7 Tips to Read a CSV File as Pandas Data Frame

Pandas is one of the popular Python package for manipulating data frames. Pandas is built on top of NumPy and thus it makes data manipulation fast and easy. Here are 7 basic options to learn to load a CSV file in pandas as data frame. Load pandas package Let us first load the pandas package. […]

Getting Started with Lambda Functions in Python

Python lets you create a function on the go, but without really assigning a name to the function. These “anonymous” functions are called “Lambda Functions”. One typically writes a lambda function on the fly, if one wants to write a function for one-time use. Lambda Functions come handy in a variety of situations and are […]

12 Basic Commands with NumPy Array

NumPy (pronounced as Num-pee or Num-pai) is one of the important python packages (other being SciPy) for scientific computing. NumPy offers fast and flexible data structures for multi-dimensional arrays and matrices with numerous mathematical functions/operations associated with it. Core data structure in NumPy is “ndarray”, short for n-dimesional array for storing numeric values. Let us […]

How to read a numerical data/file in Python with numpy?

Often you may need to read a file containing numerical data in Python for. One of the options is to import the file/data in Python is use Python’s NumPy library. There are number of advantages to use NumPy. NumPy is designed to deal with numerical data, it is fast and it has loads of built-in […]

How to read entire text file in Python?

Often one might need to read the entire content of a text file (or flat file) at once in python. In this post, we showed an example of reading the whole file and reading a text file line by line. Here is another way to import the entire content of a text file.

How to Fold a Long Single Line Text to Multiple Lines (in Python and Linux)?

You might want to convert a really long string in a single line into multiple lines with specified width. For example, if you have string like 12345678912345678912345678912345678912345678910 and want to split the long text to multi-line text, where each line is of length w, like w=9 123456789 123456789 123456789 123456789 123456789 10 In the above […]

“with” statement in Python to Open a file

A common way to work with files in Python is to create file handler with “open” statement and work with the file. After finishing the work with the file, we need to close the file handler with close statement. For example, if we want to read all lines of a file using Python¬†, we use […]

How to Run a Shell Command from Python and Get The Output?

In Python, often you may want to execute linux command and get the output of the command as string variable. Naive way to do that is to execeute the linux command, save the output in file and parse the file. A better way to get the output from executing a linux command in Python is […]