2

So im walking thourhg the pandas manual and I dont understand what im doing wrong. Could somebody please help? I mean this is standard code from the manual, except that I use a different random function I believe.

http://pandas.pydata.org/pandas-docs/dev/advanced.html --> sample window #3

import numpy as np
from pandas import DataFrame
import random

arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
         np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]

df = DataFrame(random.random(), index=arrays)

print(df)

>>>DataFrame constructor not properly called!<<<
0

1 Answer 1

3

pandas is using np.random.randn which returns an array. You are passing a single float value using random.random():

In [49]: import numpy as np
In [50]: randn = np.random.randn
In [51]: randn(8)
Out[51]: 
array([ 1.5530158 , -0.08940148,  0.10467891, -0.05558743, -0.90833863,
       -1.05916262,  0.02431885, -2.08940353])    
In [52]: from random import random    
In [53]: random()
Out[53]: 0.697960149473455

If you look at the Dataframe documentation you see what can be passed as data:

Parameters :
data : numpy ndarray (structured or homogeneous), dict, or DataFrame Dict can contain Series, arrays, constants, or list-like objects

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, in Python my struggle seems to be what do I get returned an array or just a single value, an array of values or a array of boolean... ;)
No worries, it will get easier ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.