Python Version Anzeigen

Als Guido van Rossum im Jahr 1991 die erste nutzbare Python Version 0.9.0 veröffentlichte konnte er nicht erwarten, dass er im Begriff war eine der einflussreichsten Programmiersprachen in der Welt zu erschaffen. Python hat eine goldene Zukunft: jede neue Python Version erweitert die vohergehende mit neuen Fähigkeiten und Funktionen. Um deine Python version anzuzeigen, führe … Read more

How to Serialize a Python Dict into a String and Back?

Problem Formulation Given a Python dictionary containing lists and other data structures. You want to store the dictionary in a file or send it over the network in a more efficient form. How to serialize a Python dictionary into a string, and then deserialize the string back to a dictionary data structure? Here’s a rough … Read more

How to End a Function Def in Python?

Problem Formulation Given a function definition in Python, starting with the keyword def: How to know when a “def” of a function ends? For example, in Java and C++, functions are enclosed with opening and closing parentheses {…}, so the ending of a function is not ambiguous. Ending a Function Syntactically In Python, whitespace indentation … Read more

How to Rename Column Names in Pandas?

Problem Formulation Given a Pandas DataFrame with column labels, and a list of new column names as strings. How to change the column names to replace the original ones? Here’s an example using the following DataFrame: You want to rename the column names [‘Col_A’, ‘Col_B’, ‘Col_C’] to [‘a’, ‘b’, ‘c’] so that the resulting DataFrame … Read more

NumPy Boolean Indexing

You can index specific values from a NumPy array using another NumPy array of Boolean values on one axis to specify the indices you want to access. For example, to access the second and third values of array a = np.array([4, 6, 8]), you can use the expression a[np.array([False, True, True])] using the Boolean array … Read more

What’s the Best NumPy Book?

Fear of missing out in data science? Data science and machine learning are taking over. Data-driven decision making penetrates every single company nowadays. Data science is indeed the “sexiest job in the 21st century“! There is one Python library which is the basis of any data science related computation you can undertake as a Python … Read more

How to Get a Windows Screenshot in Python?

Problem Formulation Say, you run a Python program on your Windows machine and you want it to take a screenshot. How to accomplish this programmatically? Method 1: Multiple Screen Shots Module To programmatically record one or more screenshots in your Python program, run the sct.shot() function from the mss module. Install the Multiple Screen Shots … Read more

[Solved] AttributeError: can’t set attribute in python

Problem Formulation You create a namedtuple object in your code and you want to overwrite one of the attribute values like you’re used to for normal objects: But, unexpectedly, Python raises an AttributeError: can’t set attribute. ? What can you do? ? Let’s understand the reason this error occurs—after that, you’ll learn the best fixes. … Read more

np.nonzero() – A Simple Guide with Video

This article explains first how the NumPy nonzero() function works. It then goes on to apply it to a practical problem on how to find array elements using the nonzero() function in NumPy in a practical data science example. Syntax numpy.nonzero(a) The np.nonzero(arr) function returns the indices of the elements of an array or Python … Read more

3 Little-Known NumPy Tricks in One Line [Python Puzzle]

Trick #1: Slicing and Slice Assignment This one-liner demonstrates the power of three interesting NumPy features and how their combination can solve a small data science problem in a clean and efficient manner. Say, you are working at a company and the accountant asks you to analyze salary data of different employees in your company. … Read more

How to Print the Exception Without Exiting Your Python Program?

Problem Formulation Given a basic Python program. How to print an exception if it occurs without exiting the program? For example, consider the following program that raises a ZeroDivisionError: division by zero. The output is: You want the program to keep running and executing the print statement after giving you a note about the exception: … Read more