How to upgrade all Python packages with pip

There isn’t a built-in flag yet, but you can use Note: there are infinite potential variations for this. I’m trying to keep this answer short and simple, but please do suggest variations in the comments! In older version of pip, you can use this instead: The grep is to skip editable (“-e”) package definitions, as suggested by @jawache. … Read more

Python Finding Prime Factors

Two part question: 1) Trying to determine the largest prime factor of 600851475143, I found this program online that seems to work. The problem is, I’m having a hard time figuring out how it works exactly, though I understand the basics of what the program is doing. Also, I’d like if you could shed some … Read more

How do I correctly clean up a Python object?

I’d recommend using Python’s with statement for managing resources that need to be cleaned up. The problem with using an explicit close() statement is that you have to worry about people forgetting to call it at all or forgetting to place it in a finally block to prevent a resource leak when an exception occurs. To use the with statement, create a class … Read more

Change column type in pandas

You have four main options for converting types in pandas: to_numeric() – provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type. (See also to_datetime() and to_timedelta().) astype() – convert (almost) any type to (almost) any other type (even if it’s not necessarily sensible to do so). Also allows you to convert to categorial types (very useful). infer_objects() – … Read more

How can I use numpy.correlate to do autocorrelation?

To answer your first question, numpy.correlate(a, v, mode) is performing the convolution of a with the reverse of v and giving the results clipped by the specified mode. The definition of convolution, C(t)=∑ -∞ < i < ∞ aivt+i where -∞ < t < ∞, allows for results from -∞ to ∞, but you obviously can’t store an infinitely long array. So it has to … Read more

How do I plot only a table in Matplotlib?

If you just wanted to change the example and put the table at the top, then loc=’top’ in the table declaration is what you need, Then adjusting the plot with, A more flexible option is to put the table in its own axis using subplots, which looks like this, You are then free to adjust the locations of the … Read more