Assume you have a series and the result for filtering armstrong numbers,
original series is 0 153 1 323 2 371 3 420 4 500 dtype: int64 Armstrong numbers are:- 0 153 2 371 dtype: int64
To solve this, we will follow the steps given below −
Define a series.
Create an empty list and set for loop to access all the series data.
Set armstrong intial value is 0 and create temp variable to store series elements one by one. It is defined below,
l = [] for val in data: armstrong = 0 temp = val
Create while loop and check temp>0 and calculate remainder by temp%10. Add armstrong value by taking cube of remainder and divide the temp value by 10 until it reaches 0 to terminate the loop. It is defined below,
while(temp>0): rem = temp % 10 armstrong = armstrong + (rem**3) temp = temp // 10
Set if condition to compare an original value against temp value. If it is matched, then append the values to the list.
if(armstrong==val): l.append(val)
Finally, check the list values present in the series using isin(). It is defined below,
data[data.isin(l)]
Example
Let’s see the below code to get a better implementation −
import pandas as pd data = pd.Series([153,323,371,420,500]) print("original series is\n", data) l = [] for val in data: armstrong = 0 temp = val while(temp>0): rem = temp % 10 armstrong = armstrong + (rem**3) temp = temp // 10 if(armstrong==val): l.append(val) print("Armstrong numbers are:-") print(data[data.isin(l)])
Output
original series is 0 153 1 323 2 371 3 420 4 500 dtype: int64 Armstrong numbers are:- 0 153 2 371 dtype: int64