Pandas Array (Pandas Series)-(4) Processing of Nan

Source: Internet
Author: User

The previous Pandas array (Pandas Series)-(3) Vectorization, said that when the two Pandas series were vectorized, if a key index was only in one of the series , the result of the calculation is nan , so what is the way to deal with nan ?

1. Dropna () method:

This method discards all values that are the result of NaN , which is equivalent to calculating only the values of the common key index:

ImportPandas as Pds1= PD. Series ([1, 2, 3, 4], index=['a','b','C','D']) S2= PD. Series ([ten, +, +], index=['C','D','e','F']) S3= s1+S2Print(S3)#Results:a nanb NaNc13.0D24.0e nanf Nandtype:float64Print(S3.dropna ())#Results:C 13.0D24.0Dtype:float64

2. fill_value Parameters:

Set the fill_value parameter to set a fill value when a series does not have a key index corresponding to the value:

S1 = PD. Series ([1, 2, 3, 4], index=['a','b','C','D']) S2= PD. Series ([ten, +, +], index=['C','D','e','F']) S3= S1.add (S2, fill_value=0)Print(S3)#Results:A 1.0b2.0C13.0D24.0e30.0F40.0Dtype:float64

In this way, although there is no 'a','b' in the S2 index key , Although there is no 'e','f' these two keys in S1 , but at the time of the operation, the operation is done with 0 to fill, or it can be set to another value.

As you can see, the difference between these two methods is that one will remove the key that is not in common with the two Series , and one will fill the value of the non-common key with the padding value.

Pandas Array (Pandas Series)-(4) Processing of Nan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.