1

I have a numpy array, which I want to convert to pandas data frame. The array looks like this:

[[-0.97755621 -0.99841427  0.87183304 ... -1.         -1.
  -1.        ]
 [-0.99420538 -0.89543119  0.58787309 ... -1.         -1.
  -1.        ]
 [-0.99960745 -0.49568521  0.70164658 ... -1.         -1.
  -1.        ]
 ...
 [-0.97433223 -0.96236376  0.83987189 ... -1.         -1.
  -1.        ]
 [-0.95304336 -0.99840312  0.70000874 ... -1.         -1.
  -1.        ]
 [-0.97433975 -0.9966319   0.95249494 ... -1.         -1.
  -1.        ]]

So the number of the columns is giant. For two columns I have a solution for this problem, which looks like this.

import numpy as np
import pandas as pd

# Creating a 2 dimensional numpy array
>>> data = np.array([[5.8, 2.8], [6.0, 2.2]])
>>> print(data)
>>> data
array([[5.8, 2.8],
       [6. , 2.2]])

# Creating pandas dataframe from numpy array
>>> dataset = pd.DataFrame({'Column1': data[:, 0], 'Column2': data[:, 1]})

Now the problem is that this solution is not scalable, since columns need to be specified manual. Do you have any idea how to make this solution scalable?

Thanks in advance

Jürgen K.
  • 3,427
  • 9
  • 30
  • 66

1 Answers1

2

add_prefix or add_suffix works pretty quickly

IN:

df = pd.DataFrame(data)
df.add_prefix('col_')

OUT:

    col_0   col_1
0   5.8 2.8
1   6.0 2.2
Michael Gardner
  • 1,693
  • 1
  • 11
  • 13