I need convert the data stored in a pandas.DataFrame into a byte string where each column can have a separate data type (integer or floating point). Here is a simple set of data: and df looks something like this: The DataFrame knows about the types of each column df.dtypes so I’d like to do something like this: This typically works
Tag: numpy
How to create a numpy array of lists?
I want to create a numpy array in which each element must be a list, so later I can append new elements to each. I have looked on google and here on stack overflow already, yet it seems nowhere to be found. Main issue is that numpy assumes your list must become an array, but that is not what I
timedelta error with numpy.longdouble dtype
I have times with dtype numpy.longdouble and when I’m trying to use that values with timedelta function I’ve got errors. But when I convert it to numpy.float64 everything is fine. Could somebody explain that behaviour? When I’m trying to see dtypes of variables they are look the similar but not the same: Edit And it’s strange that it’s not working
Read in all csv files from a directory using Python
I hope this is not trivial but I am wondering the following: If I have a specific folder with n csv files, how could I iteratively read all of them, one at a time, and perform some calculations on their values? For a single file, for example, I do something like this and perform some calculations on the x array:
Viewing .npy images
How can I view images stored with a .npy extension and save my own files in that format? Answer .npy is the file extension for numpy arrays – you can read them using numpy.load: One of the easiest ways to view them is using matplotlib’s imshow function: You could also use PIL or pillow: These functions aren’t part of the
How to get value counts for multiple columns at once in Pandas DataFrame?
Given a Pandas DataFrame that has multiple columns with categorical values (0 or 1), is it possible to conveniently get the value_counts for every column at the same time? For example, suppose I generate a DataFrame as follows: I can get a DataFrame like this: How do I conveniently get the value counts for every column and obtain the following
How to copy a 2D array into a 3rd dimension, N times?
I’d like to copy a numpy 2D array into a third dimension. For example, given the 2D numpy array: convert it into a 3D matrix with N such copies in a new dimension. Acting on arr with N=3, the output should be: Answer Probably the cleanest way is to use np.repeat: Having said that, you can often avoid repeating your
how to multiply pandas dataframe with numpy array with broadcasting
I have a dataframe of shape (4, 3) as following: I want to multiply each column of the dataframe with a numpy array of shape (4,): In numpy, the following broadcasting trick works: However, it doesn’t work in the case of pandas dataframe, I get the following error: Any suggestions? Answer I find an alternative way to do the multiplication
Plotting a NACA 4-series airfoil
I’m trying to plot an airfoil from the formula as described on this wikipedia page. This Jupyter notebook can be viewed on this github page. The result looks like . I expected it to look more like . Questions: Why is the line not completely smooth? There seems to be a discontinuity where the beginning and end meet. Why does
How can I draw lines into numpy arrays?
I would like to be able to draw lines into numpy arrays to get off-line features for on-line handwriting recognition. This means I don’t need the image at all, but I need for some positions in a numpy array who an image of a given size would look like. I would like to be able to specify an image size