One difference that I see here is that you (effectively) initialized df2 and df4 with dtype = int64, but df and df3 with dtype = object. You can initialize empty real values like this for df2 and df4:
#df has multiindex df = pd.DataFrame(np.empty([ncols,nlines]), columns = columns, index = lines)
You can also add dtype=int to initialize as integers, and not for realities, but that doesn't look like speed.
I get a much faster timing than you did for df4 (no difference in code), so it's a mystery to me. In any case, with the above changes to df and df3, the timings are close for df2 to df4, but unfortunately df is still pretty slow.
%timeit df.loc[(0, 0, 0), (0, 0)] = 2*np.arange(ncols) 1 loop, best of 3: 418 ms per loop %timeit df2.loc[:,0] = 2*np.arange(ncols) 10000 loops, best of 3: 185 µs per loop %timeit df3.loc[0] = 2*np.arange(ncols) 10000 loops, best of 3: 116 µs per loop %timeit df4.loc[:,0] = 2*np.arange(ncols) 10000 loops, best of 3: 196 µs per loop
Edit to add:
How much is your big problem with multi-index, I don't know, but 2 thoughts:
1) Extending @ptrj's comment, I get a very quick time for suggesting it (about the same as simple index methods):
%timeit df.loc[(0, 0, 0) ] = 2*np.arange(ncols) 10000 loops, best of 3: 133 µs per loop
So, again I get from you a completely different time (?). And FWIW, when you need an entire row with loc / iloc, it is recommended to use : rather than leaving the column reference empty:
timeit df.loc[(0, 0, 0), : ] = 2*np.arange(ncols) 1000 loops, best of 3: 223 µs per loop
But, as you can see, this is a bit slower, so I don’t know how to suggest it. I assume that you should do this as recommended in the documentation, but on the other hand, this can be an important speed difference for you.
2) As an alternative, this is rather brute force-ish, but you can just keep your index / columns, reset the index / columns should be simple, and then set the index / columns back to multi. Although, this is not completely different from the fact that you accept df.values , and I suspect that it is not convenient for you.