Indole
Indole

Reputation: 45

Python: DataFrame constructor not properly called

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!<<<

Upvotes: 2

Views: 13130

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

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

Upvotes: 3

Related Questions