Found 33676 Articles for Programming

Write a program in Python to count the total number of integer, float and object data types in a given series

Vani Nalliappan
Updated on 24-Feb-2021 06:16:08

727 Views

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

Write a program in Python to check if a series contains duplicate elements or not

Vani Nalliappan
Updated on 24-Feb-2021 06:12:06

152 Views

Input − Assume, you have the following series, 0    1 1    2 2    3 3    4 4    5The above series contains no duplicate elements. Let’s verify using the following approaches.Solution 1Assume, you have a series with duplicate elements0    1 1    2 2    3 3    4 4    5 5    3Set if condition to check the length of the series is equal to the unique array series length or not. It is defined below, if(len(data)==len(np.unique(data))):    print("no duplicates") else:    print("duplicates found")Exampleimport pandas as pd import numpy as np data = ... Read More

Write a program in Python to filter only integer elements in a given series

Vani Nalliappan
Updated on 24-Feb-2021 06:12:37

549 Views

Input − Assume you have the following series −0    1 1    2 2    python 3    pandas 4    3 5    4 6    5Output − The result for only integer elements are −0    1 1    2 4    3 5    4 6    5Solution 1Define a Series.Apply lambda filter method inside a regular expression to validate digits and expression accepts only strings so convert all the elements into strings. It is defined below,    data = pd.Series(ls)    result = pd.Series(filter(lambda x:re.match(r"\d+", str(x)), data))Finally, check the values using the isin() function.ExampleLet us ... Read More

Write a program in Python to replace all odd index position in a given series by random uppercase vowels

Vani Nalliappan
Updated on 24-Feb-2021 06:13:02

268 Views

Input − Assume, you have a Series, 0    1 1    2 2    3 3    4 4    5Output −And, the result after replacing odd index with uppercase vowels as follows −0    1 1    A 2    3 3    U 4    5SolutionDefine a Series.Define uppercase alphabetsCreate lambda filter method and replace vowels in all index positions. It is defined belowvowels = re.findall(r'[AEIOU]', chars) result = pd.Series(filter(lambda x: r.choice(vowels) if(x%2!=0), l)data)Exampleimport pandas as pd import random as r l = [1, 2, 3, 4, 5] data = pd.Series(l) print(“Given series:”, data) vowels = list("AEIOU") ... Read More

Write a program in Python to filter valid dates in a given series

Vani Nalliappan
Updated on 24-Feb-2021 06:13:28

225 Views

Input − Assume, we have a Series, 0 2010-03-12 1 2011-3-1 2 2020-10-10 3 11-2-2Output − And, the result for valid dates in a series is, 0 2010-03-12 2 2020-10-10Solution 1Define a Series.Apply lambda filter method to validate a pattern in a series, data = pd.Series(l) result = pd.Series(filter(lambda x:re.match(r"\d{4}\W\d{2}\W\d{2}", x), data))Finally, check the result to the series using the isin() function.ExampleLet us see the following implementation to get a better understanding.import pandas as pd import re l = ['2010-03-12', '2011-3-1', '2020-10-10', '11-2-2'] data = pd.Series(l) for i, j in data.items():    if(re.match(r"\d{4}\W\d{2}\W\d{2}", j)):       print(i, j)Output0   ... Read More

Write a program in Python to generate five random even index lowercase alphabets in a series

Vani Nalliappan
Updated on 24-Feb-2021 06:14:05

138 Views

Solution 1Define lowercase alphabets in a list.Create for loop and find the even index elements and add it to another list. It is defined below   ls = []    for i in l:       if(l.index(i)%2==0):          ls.append(i)Finally, apply random sample 5 values to the list and generate a series.Exampleimport pandas as pd import string import random as r chars = string.ascii_lowercase print("lowercase alphabets are:-", chars) chars_list = list(chars) data = r.sample(chars_list[::2], 5) print("random even index char's are:-", data) result = pd.Series(data) print("Series:", result)Outputlowercase alphabets are:- abcdefghijklmnopqrstuvwxyz random even index char's are:- ['w', 'k', 'i', 'u', ... Read More

Python program to filter perfect squares in a given series

Vani Nalliappan
Updated on 24-Feb-2021 06:03:23

600 Views

Input −Assume you have a series, 0    14 1    16 2    30 3    49 4    80Output −The result for perfect square elements are, 0    4 1    16 3    49Solution 1We can use regular expression and lambda function filter method to find the perfect square values.Define a Series.Apply lambda filter method to check the value is a perfect square or not. It is defined below,    l = [14, 16, 30, 49, 80]    data=pd.Series([14, 16, 30, 49, 80])    result =pd.Series(filter(lambda x: x==int(m.sqrt(x)+0.5)**2, l))Finally, check the list of values to the series ... Read More

How to use Python Pandas to find the total number of count for more than one special characters present in each word in a given series?

Vani Nalliappan
Updated on 10-Feb-2021 12:39:30

632 Views

Input − Assume you have a series, 0       fruits!! 1       *cakes* 2       $nuts 3       #drinks dtype: objectInput − The result for the total number of counts for more than one special character in a series is 2.Let us try to find different solutions to this question.Solution 1To solve this, we will follow the steps given below −Define a SeriesCreate special characters list of values.Set the initial value of special character and total special char count value as 0.Create a for loop and access all the values in the ... Read More

How to create reverse of a number in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:32:29

2K+ Views

To create reverse of a number, we can use stri_reverse function of stringi package. For example, if we have a vector called x that contain some numbers then the reverse of these numbers will be generated by using the command stri_reverse(x). But the output will be in character form, if we want to have numeric values then as.numeric function can be used.library(stringi)Example Live Demox1

How to create combination of multiple vectors in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 11:21:59

3K+ Views

To create combination of multiple vectors, we can use expand.grid function. For example, if we have six vectors say x, y, z, a, b, and c then the combination of vectors can be created by using the command expand.grid(x,y,z,a,b,c).Example Live Demox1

Advertisements