0

I have a numpy array with the following shape:

numpy_x.shape
(9982, 26)

I want turn this numpy to dataframe:

x_new_df = pd.DataFrame(numpy_x)

And my new dataframe is:

x_new_df

enter image description here

How to can I generate the dataframe with index and columns names?

bgarcial
  • 2,915
  • 10
  • 56
  • 123
  • If need custom columns or index use `x_new_df = pd.DataFrame(numpy_x, index=idx, columns=cols)`, `idx` and `cols` are lists or 1d numpy arrays with same size as `numpy_x.shape` – jezrael May 08 '18 at 14:50
  • Hi @jezrael thanks for the edition and answer comment, but I get `NameError: name 'idx' is not defined`, Should I define some equvalence to `idx` and `cols` or their values are accesed by default? – bgarcial May 08 '18 at 15:04
  • OK, so what is problem? Need some another columns names and index like default? – jezrael May 08 '18 at 15:06
  • My original dataset have some column names, and I want include them when I turn the numpy array to dataframe, here the columns of my original dataset https://cldup.com/0mzvGznzV3-3000x3000.png then I've filled the missing values and the output that I get is a numpy array, this numpy array I want translate to dataframe, but with the column names. Currently I cannot get the column names when I translate it. – bgarcial May 08 '18 at 15:28
  • Is possible extrcat columns names to list or 1d numpy array? – jezrael May 08 '18 at 15:50
  • Yes, is possible – bgarcial May 08 '18 at 16:16
  • 1
    Super, the use `x_new_df = pd.DataFrame(numpy_x, columns=cols)` where `cols` are list or `numpy 1d array` – jezrael May 08 '18 at 16:17
  • @jezrael I don't understand what list type should be cols – bgarcial May 23 '18 at 06:48
  • 1
    it is plain list, L= ['a','b','c'] – jezrael May 23 '18 at 06:49

0 Answers0