Firstly you want np.random.random_integers, secondly hstack takes a tuple so pass a tuple, thirdly you need to return something that it can align with so in this case a Series:
In [213]:
df = pd.DataFrame(np.full((6), 1))
def func(row):
l = np.full((np.random.random_integers(5)), 1)
return pd.Series(np.hstack((l, row)))
In [214]:
df.apply(func, axis=1)
Out[214]:
0 1 2 3 4 5
0 1.0 1.0 1.0 NaN NaN NaN
1 1.0 1.0 NaN NaN NaN NaN
2 1.0 1.0 NaN NaN NaN NaN
3 1.0 1.0 1.0 NaN NaN NaN
4 1.0 1.0 1.0 1.0 1.0 NaN
5 1.0 1.0 1.0 1.0 1.0 1.0
Note that I get a ton of warnings about the above:
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(3, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(2, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(1, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(4, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\numpy\core\numeric.py:301: FutureWarning: in the future, full(5, 1) will return an array of dtype('int32')
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
To get a np array from a df call attribute values:
df.apply(func, axis=1).values
random_integetis not a np function can you update your code with something that runs