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

1
  • 1
    pd.DataFrame(data, columns=['Column'+str(i+1) for i in range(data.shape[1])])? Commented Sep 17, 2019 at 19:10

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.