简体   繁体   中英

IndexError: too many indices for array. Numpy + Pandas DataFrame

I expect the DataFrame to output in an 'Excel' type of fashion, but instead, get the index error:

'IndexError: too many indices for array'

import numpy as np
import pandas as pd
from numpy.random import randn

rowi = ['A', 'B', 'C', 'D', 'E']
coli = ['W', 'X', 'Y', 'Z']
df = pd.DataFrame(randn[5, 4], rowi, coli)  # data , index , col
print(df)

How do I solve the problem?

Is this what you want:

df = pd.DataFrame(randn(5, 4), rowi, coli)

Out[583]:
          W         X         Y         Z
A -0.630006 -0.033165 -1.005409 -0.827504
B  0.044278  0.526636  1.082062 -1.664397
C  0.523847 -0.688798 -0.626712  0.149128
D  0.541975 -1.448316 -0.961484 -0.526547
E  0.066888  0.238089  1.180641  0.462298

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM