Python Traceback (most recent call last)

You are using Python 2 for which the input() function tries to evaluate the expression entered. Because you enter a string, Python treats it as a name and tries to evaluate it. If there is no variable defined with that name you will get a NameError exception. To fix the problem, in Python 2, you can use raw_input(). This returns … Read more

Get unique values from a list in python [duplicate]

First declare your list properly, separated by commas. You can get the unique values by converting the list to a set. If you use it further as a list, you should convert it back to a list by doing: Another possibility, probably faster would be to use a set from the beginning, instead of a … Read more

Can’t fix “zipimport.ZipImportError: can’t decompress data; zlib not available” when I type in “python3.6 get-pip.py”

I also came across this problem (while creating a simple installer for pyenv). Here’s how I solved it for Mac and Linux: Ubuntu 20.04, 18.04 You need the zlib development files, and probably zlib itself too: If you’re missing zlib, it’s likely that the next problem you’ll run into is with openssl, so it’s probably best to get … Read more

How does functools partial do what it does?

Roughly, partial does something like this (apart from keyword args support etc): So, by calling partial(sum2, 4) you create a new function (a callable, to be precise) that behaves like sum2, but has one positional argument less. That missing argument is always substituted by 4, so that partial(sum2, 4)(2) == sum2(4, 2) As for why it’s needed, there’s a variety of cases. … Read more

DataFrame constructor not properly called

The pd.DataFrame constructor does not accept a dictionary view as data. You can convert to list instead. Here’s a minimal example: The docs do suggest this: data : numpy ndarray (structured or homogeneous), dict, or DataFrame Equivalently, you can use pd.DataFrame.from_dict, which accepts a dictionary directly:

Convert pandas dataframe to NumPy array

df.to_numpy() is better than df.values, here’s why.* It’s time to deprecate your usage of values and as_matrix(). pandas v0.24.0 introduced two new methods for obtaining NumPy arrays from pandas objects: to_numpy(), which is defined on Index, Series, and DataFrame objects, and array, which is defined on Index and Series objects only. If you visit the v0.24 docs for .values, you will see a big red warning that says: Warning: We recommend … Read more

What is the best project structure for a Python application?

Doesn’t too much matter. Whatever makes you happy will work. There aren’t a lot of silly rules because Python projects can be simple. /scripts or /bin for that kind of command-line interface stuff /tests for your tests /lib for your C-language libraries /doc for most documentation /apidoc for the Epydoc-generated API docs. And the top-level directory can contain README’s, Config’s and whatnot. … Read more