Write millions of lines to a file - Python, Dataframes and Redis
I'm not a hard core Pythonista, but a few points I can think of are:


The assignments of key1, val1, ... appear unneeded as they're used only once in the call to format()
Terminate each if with a continue, or use else, as it appears that only one branch is executed in each iteration of the range. Order the conditions according to data's expected distribution if possible (i.e. most frequently evaluated to true condition comes first)
Try writing less, e.g. see if you can use MSET to shave a few bytes in each iteration (times millions this may have significant effects ;))


Lastly, it looks that you're not using the last two arguments you pass to format, so if that isn't a typo you can remove them as well.