The form returned by the Pandas ValueError does not match the dataframe form?

My understanding pd.DataFrame().shapereturns (n_rows, n_columns) . However, when building a data frame, the indices do not coincide with the data form, pandas calls ValueErrorwith the form like (n_columns, n_rows) .

Example:

df_2 = pd.DataFrame(np.random.randn(10,2), index = range(9))

ValueError: The form of the passed values ​​(2, 10), indexes imply (2, 9)

Why ValueError does not print :

The shape of the past values ​​is (10, 2), from the indices it follows (9, 2)

Pandas Version: '0.17.1'

+4
source share
2 answers

When pandas says “indexes” here, it means index and columns (they are both of type Index).

In [11]: df = pd.DataFrame(np.random.randn(3,2))

In [12]: df.index
Out[12]: Int64Index([0, 1, 2], dtype='int64')

In [13]: df.columns
Out[13]: Int64Index([0, 1], dtype='int64')

- 9 .index Index 2 .columns Index, , ...
, :

In [21]: df = pd.DataFrame(np.random.randn(10,2), index=np.arange(9), columns=np.arange(2))
ValueError: Shape of passed values is (2, 10), indices imply (2, 9)

:

df = pd.DataFrame(np.random.randn(10,2), index=np.arange(10), colummns=np.arange(2))
# equivalently
df = pd.DataFrame(np.random.randn(10,2), index=np.arange(10))
df = pd.DataFrame(np.random.randn(10,2))
+1

print range(9) : [0, 1, 2, 3, 4, 5, 6, 7, 8], (10,2) [ "10 2" (20 10)] , 9 , "" Numpy, Pandas DataFrame.

0

Source: https://habr.com/ru/post/1620929/


All Articles