. Advertisement .
..3..
. Advertisement .
..4..
Are you struggling to know how to handle the data? Don’t worry! In this article, we will introduce you in detail the steps to Convert NumPy Array to Pandas DataFrame. It’s very simple because this article explains how to do that. Read on to learn more!
What is a Numpy Array ?
Numpy Array is a central data structure of the library. It’s a grid of values, and it contains information about the raw data, how to locate an element, and how to interpret an element. It has a grid of elements that can be indexed in various ways and are usually non-negative integers.
For example:
import numpy as np
dep = np.array((2, 4, 6, 8, 10))
print(dep)
output
[2 4 6 8 10]
What is Pandas Dataframe ?
A Pandas dataframe is a two-way labeled data structure with columns and rows like a spreadsheet or table.
syntax:
pd.DataFrame(data, index, columns, dtype)
For example:
import pandas as pd
kt = ['white', 'black', 'pink', 'brown']
df = pd.DataFrame(kt)
print(df)
output:
0 white
1 black
2 pink
3 brown
How to Convert NumPy Array to Pandas DataFrame?
After you have understood the above concepts about numpy and dataframe, below we will show you step by step so that you can Convert NumPy Array to Pandas DataFrame easily.
Here are steps:
- Create numpy array
- Create index and column values for DataFrame.
- Create a data frame.
- Display data frame.
Now, let’s go into specific examples.
Case 1. Arrays containing only numeric data:
You create a numpy array like this (note when creating you should only leave integer values):
import numpy as np
ss_array = np.array([[10,20,30],[40,50,60]])
print(ss_array)
print(type(ss_array))
If you run python as numpy array, it will be displayed like below:
[[10 20 30]
[40 50 60]]
<class 'numpy.ndarray'>
To Convert NumPy Array to Pandas DataFrame, you would run the same syntax in this program:
import numpy as np
import pandas as pd
ss_array = np.array([[10,20,30],[40,50,60]])
df = pd.DataFrame(ss_array, columns = ['Column_E','Column_F','Column_G'])
print(df)
print(type(df))
Thus, the display result of the dataframe will be as follows:
Column_E Column_F Column_G
0 10 20 30
1 40 50 60
<class 'pandas.core.frame.DataFrame'>
Also, if you want to index the dataframe, edit the program and add an index like this:
import numpy as np
import pandas as pd
ss_array = np.array([[10,20,30],[40,50,60]])
df = pd.DataFrame(ss_array, columns = ['Column_E','Column_F','Column_G'], index = ['store_1', 'store_2'])
print(df)
print(type(df))
now the index of the dataframe will change to:
Column_E Column_F Column_G
store_1 10 20 30
store_2 40 50 60
<class 'pandas.core.frame.DataFrame'>
Case 2: The array contains both string and numeric data
Here is an example:
import numpy as np
ss_array = np.array ([['Jack', 20,1999], ['Mike', 22,1997], ['Bob', 20,1999]], dtype = object)
print (ss_array)
print (type (ss_array))
print (ss_array.dtype)
After converting to dataframe we have:
import numpy as np
import pandas as pd
ss_array = np.array([['Jack', 20,1999], ['Mike', 22,1997], ['Bob', 20,1999]], dtype=object)
df = pd.DataFrame(ss_array, columns = ['Name','Age','Birth Year'])
print(df)
print(type(df))
output:
Name Age Birth Year
0 Jack 20 1999
1 Mike 22 1997
2 Bob 20 1999
<class 'pandas.core.frame.DataFrame'>
Conclusion
We hope you enjoyed our article about discovering the answer for your lesson “Convert NumPy Array to Pandas DataFrame”. If you have any questions or concerns, please feel free to leave a comment. We are always excited when our posts can provide useful information!
Leave a comment