0

I have created a 2D array with numpy and pandas as below:

import numpy as np
import pandas as pd 

data = np.array([['','A','B','C'],
            ['0','1','2','3'],
            ['1','4','5','6'],
            ['2','7','8','9']])

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

My question is that are there any other simpler way to create a 2D array in numpy and use pandas to put it into dataframe?

3
  • 1
    What do you mean by an easier way to create a numpy array? Commented Aug 15, 2018 at 2:09
  • 1
    And yeah, don't put your columns and index into the same array of course. Usually data is numeric and columns are text. Index is often numeric but of a completely different type than the data. They don't belong together. Commented Aug 15, 2018 at 2:10
  • @MadPhysicist What i mean is that when I have more data to input (sure there will be another way to do this when you have for example 1000000 data), i don't want to type like shown. So that I want to ask for a simpler way to get the same output Commented Aug 15, 2018 at 2:23

3 Answers 3

3
In [131]: data = np.arange(1,10).reshape(3,3)
In [132]: data
Out[132]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
In [133]: df = pd.DataFrame(data=data, columns=['A','B','C'])
In [134]: df
Out[134]: 
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9
In [135]: df.dtypes
Out[135]: 
A    int64
B    int64
C    int64
dtype: object
Sign up to request clarification or add additional context in comments.

Comments

2

I think you do not need to assign the index since it is from 0 to len(df), also using the numpy array with mix data type will cause problem , since you output data type is object not int

pd.DataFrame({'A':[1,4,7],'B':[2,5,8],'C':[3,6,9]})
Out[1104]: 
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9

3 Comments

Good answer ;) But how to get from data to the dict?
@RafaelC IIUC *any other simpler way to create a 2D array in numpy * is he need a new way for creating the dataframe ?
Not sure wen.. I think he wants to create a data frame from data in a simpler way than using pd.DataFrame(data=data[1:,1:], index=data[1:,0], columns=data[0,1:])
1

Or an direct list:

>>> import pandas as pd
>>> pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=list('ABC'))
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9
>>> 

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.