Python for Data Analytics: A Beginners Guide for Learning Python Data Analytics from A-Z by Root Alex

Python for Data Analytics: A Beginners Guide for Learning Python Data Analytics from A-Z by Root Alex

Author:Root, Alex [Root, Alex]
Language: eng
Format: epub
Published: 2019-09-04T16:00:00+00:00


Important Functionalities

We will see the basic and important mechanisms of data interaction in DataFrame and series in this section. We will not focus on all the documentation present in the pandas library; but instead we will see the most important and frequently used functionalities.

Re-indexing

An important function to perform on pandas objects is to re-index. It is a process in which we create a new object and the data follows to the new index. Let us see an example:

In [ 1 ]: first_obj = Series( [2.4, 5.1, 3.7, 7.6], index = ['v', 'u', 'w', 'x'] )

In [ 2 ]: first_obj

Out [ 2 ]:

v 2.4

u 5.1

w 3.7

x 7.6

Now we will call the reindex function on the first object. It will rearrange all the data as per the new index.

In [ 3 ]: new_obj = obj.reindex( ['u', 'v', 'w', 'x', 'y'] )

In [ 4 ]: new_obj

Out [ 4 ]:

u 5.1

v 2.4

w 3.7

x 7.6

y NaN

We can also set the missing values to 0 if we use the fill_value argument inside the function.

In [ 5 ]: newobj.reindex( ['u', 'v', 'w', 'x', 'y' ], fill_value = 0)

Out[ 5 ]:

u 5.1

v 2.4

w 3.7

x 7.6

y 0.0

We can also use the arguments ffill and bfill to carry values forward or carry values backwards, respectively.

Here are some of the arguments that can be used with the reindex function.

Argument name

Description of the argument



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.