Tengo la siguiente matriz numpy llamada 'datos':

array([['ksr-usconeng101', 'C', '632.3', '1'],
       ['ksr-usconeng101', 'D', '242.9', '2'],
       ['ksr-usconeng158', 'C', '1044.5', '3'],
       ['ksr-usconeng158', 'D', '2771.2', '4'],
       ['ksr-usconeng158', 'G', '7.3', '5'],
       ['ksr-usconeng163', 'C', '1597.0', '6'],
       ['ksr-usconeng163', 'D', '1676.3', '7'],
       ['server', 'drive', 'size', '']],
      dtype='<U15')

Estoy tratando de convertirlo en un marco de datos:

pd.DataFrame(data=data[0:-1,0:3],
                   index = data[0:-1,-1],
                   columns = data[-1:, 0:-1])

Datos -

data[0:-1,0:3]
Out[145]: 
array([['ksr-usconeng101', 'C', '632.3'],
       ['ksr-usconeng101', 'D', '242.9'],
       ['ksr-usconeng158', 'C', '1044.5'],
       ['ksr-usconeng158', 'D', '2771.2'],
       ['ksr-usconeng158', 'G', '7.3'],
       ['ksr-usconeng163', 'C', '1597.0'],
       ['ksr-usconeng163', 'D', '1676.3']],
      dtype='<U15')

Índice -

data[0:-1,-1]
Out[146]: 
array(['1', '2', '3', '4', '5', '6', '7'],
      dtype='<U15')

Columnas

data[-1:, 0:-1]
Out[147]: 
array([['server', 'drive', 'size']],
      dtype='<U15')

Sin embargo, python no está de acuerdo y responde con:

ValueError: Shape of passed values is (3, 7), indices imply (1, 7)

Por favor sugiera lo que me estoy perdiendo ...

0
Kuber Gaur 22 feb. 2018 a las 21:30

3 respuestas

La mejor respuesta

Las columnas deben ser 1D:

df = pd.DataFrame(data=data[:-1,:3],
                  index=data[:-1,-1],
                  columns=data[-1, :-1])
print(df)

Salida:

         server drive    size
1  ksr-usconeng101     C   632.3
2  ksr-usconeng101     D   242.9
3  ksr-usconeng158     C  1044.5
4  ksr-usconeng158     D  2771.2
5  ksr-usconeng158     G     7.3
6  ksr-usconeng163     C  1597.0
7  ksr-usconeng163     D  1676.3

Tienes:

>>> data[-1:, 0:-1].shape
(1, 3)

Pero necesita:

>>> data[-1, :-1].shape
(3,)
1
Mike Müller 22 feb. 2018 a las 18:40

Prueba esto

pd.DataFrame(data=data[0:-1,0:3],
                   index = data[0:-1,-1],
                   columns = data[-1:, 0:-1].tolist())
0
Rayadurai 22 feb. 2018 a las 18:47
import  numpy as np, pandas as pd

df = pd.DataFrame(data[0:7, 0:3].flatten().reshape(7,3),
       columns = ["a", "b", "c"])

            a           b     c
0   ksr-usconeng101     C   632.3
1   ksr-usconeng101     D   242.9
2   ksr-usconeng158     C   1044.5
3   ksr-usconeng158     D   2771.2
4   ksr-usconeng158     G   7.3
5   ksr-usconeng163     C   1597.0
6   ksr-usconeng163     D   1676.3
0
MichaelRSF 22 feb. 2018 a las 18:43