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