| by Arround The Web | No comments

Pandas Array to DataFrame

This Pandas tutorial will teach you how to turn a NumPy array into a Pandas DataFrame object. The values in multiple dimensions can be stored using multidimensional arrays. A 3D array can be compared to a cube while an array in two dimensions can be compared to a matrix.

The pandas.DataFrame() constructor is used to convert a NumPy array, similar to converting a dictionary.

Syntax:
DataFrame_object=pandas.DataFrame(numpy_array,index=[‘a’,’b’], columns=[‘num’,’chr’])

Parameters:

  1. The first parameter, data, is the only one necessary. The array, which is required to turn into a data frame, is now placed here.
  2. Index: This is an index or an array-like index. If we don’t use this parameter, the RangeIndex is used by default.
  3. columns: These are labels for the columns in an index or array-like data frame. Again, RangeIndex (0, 1, 2,…, n) is used by default if we don’t use this argument.

Example 1:
Let’s have a NumPy array named “actual” having 5 rows and convert it into DataFrame by passing the NumPy array.

import pandas
import numpy

# Consider the numpy array
actual=numpy.array([[1,"cooking",200],[2,"music",3004],[3,"hand loom",1000],[4,"hand loom",2000],[5,"dressing",3000]])

print("Numpy Array: ",actual,"\n")

# Convert the above array to the pandas DataFrame
converted=pandas.DataFrame(actual)

# Display the converted DataFrame
print(converted)

Output:

 Numpy Array:  [['1' 'cooking' '200']
 ['2' 'music' '3004']
 ['3' 'hand loom' '1000']
 ['4' 'hand loom' '2000']
 ['5' 'dressing' '3000']]

   0          1     2
0  1    cooking   200
1  2      music  3004
2  3  hand loom  1000
3  4  hand loom  2000
4  5   dressing  3000

Explanation:
After converting to the Pandas DataFrame, the indices are [0,1,2,3,4] and the columns are [0,1,2].

Example 2: With Columns Parameter
Now, we pass the column names to the DataFrame along with the NumPy array.

import pandas
import numpy

# Consider the numpy array
actual=numpy.array([[1,"cooking",200],[2,"music",3004],[3,"hand loom",1000],[4,"hand loom",2000],[5,"dressing",3000]])

# Convert the above array to the pandas DataFrame by passign column names
converted=pandas.DataFrame(actual, columns = ['id','work','wages'])

# Display the converted DataFrame
print(converted)

Output:

  id       work wages
0  1    cooking   200
1  2      music  3004
2  3  hand loom  1000
3  4  hand loom  2000
4  5   dressing  3000

Explanation:
Now, you can see the column names in the converted DataFrame. They are [“id”,”work”,”wages”].

Example 3: With Index Parameter
Now, we pass the index values, index=[‘person 1′,’person 2′,’person 3′,’person 4′,’person 5’], to the DataFrame along with the NumPy array to the index parameter.

import pandas
import numpy

# Consider the numpy array
actual=numpy.array([[1,"cooking",200],[2,"music",3004],[3,"hand loom",1000],[4,"hand loom",2000],[5,"dressing",3000]])

# Convert the above array to the pandas DataFrame by adding index
converted=pandas.DataFrame(actual, columns = ['id','work','wages'],index=['person 1','person 2','person 3','person 4','person 5'])

# Display the converted DataFrame
print(converted)

Output:

         id       work wages
person 1  1    cooking   200
person 2  2      music  3004
person 3  3  hand loom  1000
person 4  4  hand loom  2000
person 5  5   dressing  3000

Explanation:
Previously, the indices were [0,1,2,3,4]. Now, you can see the indices for each row.

Example 4: Convert a Particular Row
Let’s have a NumPy array with 2 rows and convert only the first row to the Pandas DataFrame.

import pandas
import numpy

# Consider the numpy array
actual=numpy.array([["health_clinic","delhi",522554],["medi view","france",434456]])

# Convert only the first row of the numpy array to the DataFrame
converted=pandas.DataFrame([actual[0]], columns = ['Hospital','Address','pincode'],index=['H1'])

# Display the converted DataFrame
print(converted)

Output:

         Hospital Address pincode
H1  health clinic   delhi  522554

Explanation:
Here, we need to pass the row index hat has to be converted to the NumPy array.
To convert only the first row to the DataFrame, we need to pass the index as 0.

Conclusion

You learned how to convert an array into a DataFrame in this Pandas tutorial. You first studied about the Pandas DataFrame objects and NumPy arrays. The syntax and DataFrame class, which we can utilize to generate the data frame objects, were discussed. Then, we looked at three instances where we transformed the NumPy arrays into Pandas DataFrames.

Share Button

Source: linuxhint.com

Leave a Reply