18 Nisan 2022 Pazartesi

Vlookup in Python (other than JOIN)

If you want to handle everything in Python (instead of using SQL or Excel), there are many vlookup possibilities in python. Here I want to show the one I used in Boston Crime Data.

I wanted to group many crime definitions to decrease the number of categories in a new "offense description" column. For example instead of having "property damage", "auto theft", "larceny - all others" categories seperately, I wanted to group those crimes in one "theft-damage" category.

I created a new df with two columns, namely "Offense_Description" and "Offense_Category". The first column included all unique values of the offense description and the second one regrouped them in new categories.

Then I used ".map" method to act as the vlookup in excel and create a new column in the dataferame. I am adding this here, because at times merge or join functions do not work as well as expected and this may be a nice option to keep in mind. Here is how it looks:


Another example with .merge method (while converting the district codes to district names):



Hiç yorum yok:

Yorum Gönder