Using a structured array

Structured arrays are slightly different from ndarrays. Each field in a structured array can be of a different data type. For more information on structured arrays, refer to the following: http://docs.scipy.org/doc/numpy/user/basics.rec.html.

The following is an example of a structured array:

memberData = np.array([('Sanjeev',37,162.4),
('Yingluck',45,137.8),
('Emeka',28,153.2),
('Amy',67,101.3)],
dtype = [('Name','a15'),
('Age','i4'),
('Weight','f4')])

This structured array has three fields for which the data types have been defined in a list of tuples along with the field names. The same DataFrame function can be used to construct a DataFrame function from a structured array:

memberDF = pd.DataFrame(memberData)

Take a look at the following output:

By default, continuous range of integral values have been assigned to the index. It is possible to replace the indices:

pd.DataFrame(memberData, index=['a','b','c','d'])

Take a look at the following output:

The columns can be reordered through the columns argument of the DataFrame function:

pd.DataFrame(memberData, columns = ["Weight", "Name", "Age"])

Take a look at the following output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.146.77