Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sapply equivalent in python?

Tags:

python

r

sapply

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?

like image 233
John Williams Avatar asked Apr 02 '16 19:04

John Williams


2 Answers

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)).

like image 139
W. Zhu Avatar answered Oct 14 '22 11:10

W. Zhu


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)])
like image 23
Parfait Avatar answered Oct 14 '22 11:10

Parfait