0
votes

I tried to use DataFrame in Python. Commands are:

import pandas as pd
from numpy.random import uniform
df = pd.DataFrame(uniform(0,1,(3,4)),
                  index = 'A B C D'.split(),
                  columns='E F G H'.split())

But unfortunately I get the following error. Does anybody have an idea how to fix this issue?

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes) 1680 -> 1681 mgr = BlockManager(blocks, axes) 1682 mgr._consolidate_inplace()

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in init(self, blocks, axes, do_integrity_check) 142 if do_integrity_check: --> 143 self._verify_integrity() 144

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in _verify_integrity(self) 344 if block._verify_integrity and block.shape[1:] != mgr_shape[1:]: --> 345 construction_error(tot_items, block.shape[1:], self.axes) 346 if len(self.items) != tot_items:

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in construction_error(tot_items, block_shape, axes, e) 1718
raise ValueError( -> 1719 "Shape of passed values is {0}, indices imply {1}".format(passed, implied) 1720 )

ValueError: Shape of passed values is (5, 4), indices imply (4, 4)

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) in 1 df = pd.DataFrame(uniform(0,1,(5,4)), 2 index = 'A B C D'.split(), ----> 3 columns='W X Y Z'.split())

~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in init(self, data, index, columns, dtype, copy) 438 mgr = init_dict({data.name: data}, index, columns, dtype=dtype) 439 else: --> 440 mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy) 441 442 # For data is list-like, or Iterable (will consume into list)

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/construction.py in init_ndarray(values, index, columns, dtype, copy) 211 block_values = [values] 212 --> 213 return create_block_manager_from_blocks(block_values, [columns, index]) 214 215

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes) 1686
blocks = [getattr(b, "values", b) for b in blocks] 1687
tot_items = sum(b.shape[0] for b in blocks) -> 1688 construction_error(tot_items, blocks[0].shape[1:], axes, e) 1689 1690

~/anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py in construction_error(tot_items, block_shape, axes, e) 1717
raise ValueError("Empty data passed with indices specified.") 1718 raise ValueError( -> 1719 "Shape of passed values is {0}, indices imply {1}".format(passed, implied) 1720 ) 1721

ValueError: Shape of passed values is (5, 4), indices imply (4, 4)

1

1 Answers

1
votes

You are creating an 3x4 matrix but providing 4 row indices. Provide only 3 rows to your index.

import pandas as pd
from numpy.random import uniform
df = pd.DataFrame(uniform(0,1,(3,4)),
              index = 'A B C'.split(),
              columns='E F G H'.split())