Skip to content

Tag: python

How to find out week no of the month in python?

I have seen many ways to determine week of the year. Like by giving instruction datetime.date(2016, 2, 14).isocalendar()[1] I get 6 as output. Which means 14th feb 2016 falls under 6th Week of the year. But I couldn’t find any way by which I could find week of the Month. Means IF I give input as some_fu…

Python fast DataFrame concatenation

I wrote a code to concatenate parts of a DataFrame to the same DataFrame as to normalize the occurrence of rows as per a certain column. and this is unbelievably slow. Is there a way to fast concatenate DataFrame without creating copies of it? Answer There are a couple of things that stand out. To begin with,…

Group by and find top n value_counts pandas

I have a dataframe of taxi data with two columns that looks like this: Basically, each row represents a taxi pickup in that neighborhood in that borough. Now, I want to find the top 5 neighborhoods in each borough with the most number of pickups. I tried this: Which gives me something like this: How do I filt…

Second newest file

I need the second newest file. In this thread the newest is found: Python get most recent file in a directory with certain extension which uses this construct: However, how can I get not the min or max but the second element? Answer I think this can be a suitable solution: So basically glob.iglob(‘*.log…

Jinja loop.index does not print

When running the following jinja code, I only get “Column info” printed. Why the index does not appears ? Answer It sounds like the template is being treated as a Django template, not a Jinja template. Using {{ loop.index }} should work in a Jinja template, but wouldn’t work in a Django temp…

Python Logging – Disable logging from imported modules

I’m using the Python logging module, and would like to disable log messages printed by the third party modules that I import. For example, I’m using something like the following: This prints out my debug messages when I do a logger.debug(“my message!”), but it also prints out the debug…