My 404s Don't code and drive

About

Pandas - How to replace string values in a column with integer numbers

I found this nice solution on StackOverflow to the problem of replacing strings in the columns of a dataframe. Lets assume that a column named labels contains the label strings [A,B,C] and we want to replace them with [0,1,2]. Then we can do the following,

labels = df['labels'].unique().tolist()
mapping = dict( zip(labels,range(len(labels))) )
df.replace({'labels': mapping},inplace=True)

This code first creates a list of the unique labels, after that it creates a dictionary mapping from label value to a numeric value, and finally does a in place replacing of the label values with the numerical values.

How to clear the terminal buffer

Sometimes you wan to start over in the terminal. On OSX on your Mac there is a convenient shortcut: cmd+k. This will clear your scroll buffer. In Ubuntu there is no such shortcut. What you can do clear and reset the scroll-back is to write

clear && printf '\e[3J'

Of course we can add an alias to our .bashrc so that we can just write, for example, ccc

alias ccc="clear && printf '\e[3J'"

When Debugging With LLDB and CMake Make Sure to Turn of Optimization

When debugging C++ using LLDB from the command line. It seems you have to put

make DCMAKE_BUILD_TYPE=debug

Just removing optimization flags and writing

set(CMAKE_BUILD_TYPE Debug)

in the CMakeLists.txt does not seem to do the trick when there are underlying CMakeLists.txt hierarchies or some other parameters. Nevertheless, by using the DCMAKE_BUILD_TYPE parameter you get access to the otherwise, by optimization, hidden variables.

Ubuntu Terminal Open Command

On OSX one can use the open command in the terminal to open a document using its associated program. Running open on a pdf file will open it using Preview. On Ubuntu the corresponding command is xgd-open. It is cumbersome to write that over and over again but we can add an alias in the .bashrc file

alias o="xdg-open"

If we, for example, want to open a pdf file we can write

o mydocument.pdf

Concatenating Numpy Vectors Stored In Files

Sometimes you have vectors or matrices stored in files, and you need to read them and concatenate them. One way of doing this is reading them into a list and then using Numpy’s hstack or vstack to create the vector or matrix. Like this for concatenating rows

result = np.hstack([np.genfromtxt(fName,delimiter=";") for fName in fList])

where the read matrix or vector all are of size MxNf, that is the rows, M, are the same across all read matrices while Nf can vary. The same goes for vertical stacking but then the sizes are reversed, that is, NfxM, and we have,

result = np.vstack([np.genfromtxt(fName,delimiter=";") for fName in fList])