简体   繁体   中英

Python DataFrame TypeError: only integer scalar arrays can be converted to a scalar index

I know there are several questions about this error already. But in this particular case I'm not sure whether there is already a solution for my problem. I have this part of code and i want to print the column "y" of the Dataframe df. The following error occurs: TypeError: only integer scalar arrays can be converted to a scalar index

labels=[]
xvectors=[]

for i in data:
    labels.append(i[0])
    xvectors.append(i[1])

X = np.array(xvectors)
y = np.array(labels)

feat_cols = [ 'xvec'+str(i) for i in range(X.shape[1]) ]
print(feat_cols)
df = pd.DataFrame(X,columns=[feat_cols])
df['y']= y
#df['label'] = df['y'].apply(lambda i: str(i))
print(df['y'])

X, y = None, None

Printing the whole DataFrame is possible. This looks like:

        xvec0     xvec1     xvec2     xvec3     xvec4  ...   xvec508   xvec509   xvec510   xvec511        y
0    3.397163 -1.112423  0.414708  0.563083  1.371336  ...  1.201095 -0.076261 -0.620443 -1.231465  DA01_03
1    0.159473  1.884818 -1.511547 -0.153500 -0.635701  ... -1.217205 -1.922081  0.878613  0.087912  DA01_06
2    1.089404  0.331919 -1.027480  0.594129 -2.473234  ... -3.505570 -3.509632 -0.553128 -0.453307  DA01_10
3    0.183993 -1.741467 -0.142570 -3.158320  4.355789  ...  3.857311  3.142393  0.991663 -2.842322  DA01_14

This is the whole errror message:

    print(df['y'])
  File "/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py", line 2958, in __getitem__
    return self._get_item_cache(key)
  File "/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py", line 3270, in _get_item_cache
    values = self._data.get(item)
  File "/usr/local/lib/python3.7/dist-packages/pandas/core/internals/managers.py", line 960, in get
    return self.iget(loc)
  File "/usr/local/lib/python3.7/dist-packages/pandas/core/internals/managers.py", line 977, in iget
    block = self.blocks[self._blknos[i]]
TypeError: only integer scalar arrays can be converted to a scalar index

I think it has something to do with the numpy array. Thank you in advance!

Ah you pass your columns argument as a list in a list ( feat_cols is already of type list). This turns your column headers 2-dimensional: you can see df.info() says it ranges from (xvec0,) to... instead of xvec0 .

Passing columns=feat_cols should do the trick:-)

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