Tag: python
-
Repeat rows in DataFrame with respect to column
7 I have a Pandas DataFrame that looks like this: df = pd.DataFrame({‘col1’: [1, 2, 3], ‘col2’: [4, 5, 6], ‘col3’: [7, 8, 9]}) df col1 col2 col3 0 1 4 7 1 2 5 8 2 3 6 9 I would like to create a Pandas DataFrame like this: df_new col1 col2 col3 0 […]
-
Unexpected uint64 behaviour 0xFFFF’FFFF’FFFF’FFFF – 1 = 0?
28 Consider the following brief numpy session showcasing uint64 data type import numpy as np a = np.zeros(1,np.uint64) a # array([0], dtype=uint64) a[0] -= 1 a # array([18446744073709551615], dtype=uint64) # this is 0xffff ffff ffff ffff, as expected a[0] -= 1 a # array([0], dtype=uint64) # what the heck? I’m utterly confused by this last […]
-
Can you explain this difference of depth recursion in Python using those seemingly equivalent codes?
6 I noticed that on my machine, the following reaches the max of depth recursion for n = 2960: m = {0:0, 1:1} def f(n): if n not in m: m[n] = f(n – 1) + f(n – 2) return m[n] while this version reaches it for n = 988: m = {0:0, 1:1} def […]
-
Why is b.pop(0) over 200 times slower than del b[0] for bytearray?
24 Letting them compete three times (a million pops/dels each time): from timeit import timeit for _ in range(3): t1 = timeit(‘b.pop(0)’, ‘b = bytearray(1000000)’) t2 = timeit(‘del b[0]’, ‘b = bytearray(1000000)’) print(t1 / t2) Time ratios (Try it online!): 274.6037053753368 219.38099365582403 252.08691226683823 Why is pop that much slower at doing the same thing? python […]