Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 766 of 855
Write a program in Python to find the missing element in a given series and store the full elements in the same series
SolutionTo solve this, we will follow the steps given below −Define a Series.Create a for loop and access the data from start to end elements. Set if condition to check the data is present or not.If the value is not in the range then append it to the list. Finally, sort and print the values.for i in range(data[0], data[length-1]): if(i not in data): l1.append(i) else: l1.append(i)ExampleLet us see the following implementation to get a better understanding.import pandas as pd import numpy as np l = [1, 2, 3, 6, 7] l1 = ...
Read MoreWrite a program in Python to find the index for NaN value in a given series
Input −Assume, you have a series, 0 1.0 1 2.0 2 3.0 3 NaN 4 4.0 5 NaNOutput − And, the result for NaN index is, index is 3 index is 5SolutionTo solve this, we will follow the steps given below −Define a Series.Create for loop and access all the elements and set if condition to check isnan(). Finally print the index position. It is defined below, for i, j in data.items(): if(np.isnan(j)): print("index is", i)ExampleLet us see the following implementation to get a better understanding.import pandas as pd import numpy as np l ...
Read MoreWrite a program in Python to generate any random five prime numbers between 100 to 150 in a Series
SolutionTo solve this, we will follow the steps given below −Define an empty listCreate a for loop and set range from 100 to 150Set another for loop to access the values from 2 to range of values and find the factors, if nothing is found then add to the list. It is defined below, for i in range(100, 150): for j in range(2, i): if(i % j == 0): break else: l.append(i)Set random sample value as 5 and assign into the list then finally create a Series.data = ...
Read MoreWrite a program in Python to filter the elements in a series which contains a string start and endswith 'a'
Input − Assume, you have a Series, 0 apple 1 oranges 2 alpha 3 aroma 4 betaOutput − And, the result for elements start and endswith ‘a’.2 alpha 3 aromaSolution 1Define a Series.Create regular expression to check start and endswith ‘a’r'^[a]$|^([a]).*\1$'Create an empty list and set for loop and set if condition inside to check the pattern. It is defined below, for i in data: if(re.search(exp, i)): ls.append(i)Finally, check the series using isin().ExampleLet us see the following implementation to get a better understanding.import pandas as pd import re l ...
Read MoreWrite a program in Python to print the power of all the elements in a given series
Input − Assume, you have a series, 0 1 1 2 2 3 3 4Output − And, the result for the power of all elements in a series is, 0 1 1 4 2 27 3 256Solution 1Define a Series.Create transform method inside apply lambda power value. It is defined below, data.transform(lambda x:x**x)data.transform(lambda x:x**x)Solution 2Define a Series.Create an empty list. Create for loop, iter all the items. Append elements to the list. It is defined below, for i, j in data.items(): ls.append(m.pow(j, j))Finally, convert the list into Series.ExampleLet us see the ...
Read MoreWrite a program in Python to remove the elements in a series, if it contains exactly two spaces
Input −Assume, you have a series, 0 This is pandas 1 python script 2 pandas seriesOutput −And, the result after removing an element contains exactly two spaces, 1 python script 2 pandas seriesSolution 1Define a Series.Create lambda filter method to apply a regular expression to find the total number of spaces not equal to 2 as follows −pd.Series(filter(lambda x:len(re.findall(r" ", x))!=2, data))Finally, check the list of values to the series using isin().Solution 2Define a Series.Create for loop to iter the elements one by one and set if condition to count the spaces equal to 2. ...
Read MoreWrite a program in Python to sort all the elements in a given series in descending order
Input − Assume, you have a Series,0 abdef 1 ijkl 2 Abdef 3 oUijlOutput − And the result for all the elements in descending order,3 oUijl 1 ijkl 0 abdef 2 AbdefSolutionTo solve this, we will follow the steps given below −Define a SeriesApply sort_values method with the argument as ascending = False. It is defined below,data.sort_values(ascending=False)ExampleThe complete code listing is as follows,import pandas as pd l=["abdef","ijkl","Abdef","oUijl"] data=pd.Series(l) print("original series: ", data) print(data.sort_values(ascending=False))Output3 oUijl 1 ijkl 0 abdef 2 Abdef
Read MoreWrite a program in Python to verify kth index element is either alphabet or number in a given series
Input − Assume, you have a Series, a abc b 123 c xyz d ijkSolutionTo solve this, we will follow the steps given below −Define a SeriesGet the index from userSet the if condition to check the value is digit or not. It is defined below, if(data[x].isdigit()): print("digits present") else: print("not present")ExampleLet us see the following implementation to get a better understanding.import pandas as pd dic = {'a':'abc', 'b':'123', 'c':'xyz', 'd':'ijk'} data = pd.Series(dic) x = input("enter the index : ") if(data[x].isdigit()): print("digits present") else: print("not present")Outputenter the index : a not ...
Read MoreWrite a program in Python to print the elements in a series between a specific range
Input − Assume, you have a series,0 12 1 13 2 15 3 20 4 19 5 18 6 11Output − The result for the elements between 10 to 15 as,0 12 1 13 2 15 6 11Solution 1Define a SeriesCreate an empty list.Create for loop to access all the elements one by one and set if condition to compare the value from above or equal to 10 and below or equal to 15. Append matched values to an empty list as follows −for i in range(len(data)): if(data[i]>=10 and data[i]=10 and data[i]
Read MoreWrite a program in Python to count the total number of integer, float and object data types in a given series
Input − Assume, you have a series, 0 1 1 2 2 python 3 3 4 4 5 5 6 6.5Output −Total number of integer, float and string elements are, integer count: 5 float count: 1 string count: 1SolutionTo solve this, we will follow the steps given below −Define a Series.Create lambda filter method to extract the length of an integer value as follows, len(pd.Series(filter(lambda x:type(x)==int, data)Create lambda fliter method to extract length of float value as follows, len(pd.Series(filter(lambda x:type(x)==float, data)Create lambda fliter method to extract length of string value as follows, len(pd.Series(filter(lambda ...
Read More