简体   繁体   中英

How Can I create a Method in a class that I can convert a numpy matrix to a Pandas Frame?

I am trying to define a method where I can convert from a numpy matrix to Pandas DataFrame.

I have the following:

import pandas as pd
import numpy as np

class Analisis():
    def __init__(self, matriz = np.array([])):
          self.__matriz = matriz
          self.__filas = matriz.shape[0]
          self.__columnas = matriz.shape[1]
    @property
    def matriz(self):
         return self.__matriz
    @property
    def filas(self):
         return self.__filas
    @property
    def columnas(self):
         return self.__columnas
    def as_data_frame (self):
        dataset = pd.DataFrame({'Columna1': data[:, 0], 'Columna2': data[:, 1], 'Columna3': 
        data[:, 2]})
        return dataset

And I am working with this array:

data = Analisis(np.array([[5,78,34],[6,2,8],[36,9,60]]))
print(data.filas)
print(data.columnas)
print(data.matriz)
Analisis.as_data_frame

But I have tried several combinations with Analisis.as_data_frame and they did not work. Just tried to find documentation and seems that method is okay, but do not work. Any idea?

Since you are defining a method you would want to call it on your object:

data.as_data_frame()

But your definition uses data , presumably the global variable. But you should be using the internal state . So, presumably, you want self.__matriz .

An aside:

Stop using double-underscores and needless properties, all of this is boilerplate that defeats the entire purpose of property to begin with. In Python, your class should look like this:

class Analisis:
    def __init__(self, matriz=None): # watch out for mutable default arguments
        if matriz is None:
            self.matriz = matriz
        else:
            self.matriz = np.array([])
        self.filas = matriz.shape[0]
        self.columnas = matriz.shape[1]

And with your now as to your method, you want:

class Analisis:

    def __init__(self, matriz=None): # watch out for mutable default arguments
        if matriz is None:
            self.matriz = np.array([])
        else:
            self.matriz = matriz
        self.filas = matriz.shape[0]
        self.columnas = matriz.shape[1]

    def as_data_frame (self):
        dataset = pd.DataFrame(
            {
                'Columna1': self.matriz[:, 0], 
                'Columna2': self.matriz[:, 1], 
                'Columna3': self.matriz[:, 2]
            }
        )
        return dataset

and you could probably simplify your method as just:

    def as_data_frame (self):
        dataset = pd.DataFrame(
            self.matriz[:,:3],
            columns=['Columna1','Columna2','Columna3']
        )
        return dataset

Note, a two-dimensional array can always be converted directly do a dataframe:

>>> arr = np.array([[5,78,34],[6,2,8],[36,9,60]])
>>> pd.DataFrame(arr, columns=['Columna1', 'Columna2', 'Columna3'])
   Columna1  Columna2  Columna3
0         5        78        34
1         6         2         8
2        36         9        60

I suspect what you really want is something more dynamic, like so:

    def as_data_frame (self):
        columns = [f'Columna{i}' for i in range(1, self.columnas+1)]
        dataset = pd.DataFrame(self.matriz, columns=columns)
        return dataset

In action:

In [10]: class Analisis:
    ...:
    ...:     def __init__(self, matriz=None): # watch out for mutable default arguments
    ...:         if matriz is None:
    ...:             self.matriz = np.array([])
    ...:         else:
    ...:             self.matriz = matriz
    ...:         self.filas = matriz.shape[0]
    ...:         self.columnas = matriz.shape[1]
    ...:
    ...:     def as_data_frame (self):
    ...:         columns = [f'Columna{i}' for i in range(1, self.columnas+1)]
    ...:         dataset = pd.DataFrame(self.matriz, columns=columns)
    ...:         return dataset
    ...:
    ...:

In [11]: data = Analisis(np.array([[5,78,34],[6,2,8],[36,9,60]]))

In [12]: data.as_data_frame()
Out[12]:
   Columna1  Columna2  Columna3
0         5        78        34
1         6         2         8
2        36         9        60

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