I need to grow a python DataFrame one row at a time.
In R, the sapply() function is fast & efficent. E.g.,
sapply(1:100, function(i) rnorm(50) )
produces a 50 x 100 matrix of (standard normal random) numbers, which can then be transposed and/or converted into a data frame, as needed
How to do same efficiently in python?
sapply in R is equivalent to map in python.
sapply(c(-1, 1), abs) in R is equivalent to map(abs, (-1, 1)) in Python. But map returns a map object, so you need to pass it to list() if you want a list. In Python, you can also use list comprehension [abs(i) for i in (-1, 1)].
Your example is not a good use case of sapply. The matrix can be achieved without using it: matrix(rnorm(5000), 50, 100). Equivalently, in Python, with import numpy as np, this can be achieved with np.random.normal(size=(50, 100)).
Consider the following list comprehension version:
import numpy as np
import pandas as pd
df = pd.DataFrame([np.random.randn(50) for i in range(100)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With