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 767 of 855
Write a program in Python to generate five random even index lowercase alphabets in a series
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 MoreWrite a program in Python to filter valid dates in a given series
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 MoreWrite a program in Python to replace all odd index position in a given series by random uppercase vowels
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 MoreWrite a program in Python to filter only integer elements in a given series
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 MoreWrite a program in Python to check if a series contains duplicate elements or not
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 MorePython program to filter perfect squares in a given series
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 MoreLargest Merge of Two Strings in Python
Let us suppose we have two strings 'a' and 'b' and a string 'merge'. The task is to fill the string 'merge' with the characters from 'a' and 'b' in such a way that, If the string 'a' is non-empty, then remove the first character from the string 'a' and copy it into string 'merge'.If the string 'b' is non-empty, then remove the first character from the string 'b' and copy it into string 'merge'.If the strings 'a' and 'b' are non-empty, then remove the first characters from string 'a' and copy it into string 'merge' and then remove the ...
Read MoreHow can Tensorflow be used with Estimator to make predictions from trained model?
Tensorflow can be used with the estimator to predict output on new data using the ‘predict’ method which is present in the ‘classifier’ method.Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. TensorFlow ...
Read MoreHow can Tensorflow be used with Estimator to compile the model using Python?
Tensorflow can be used with the estimator to compile the model with the help of the ‘train’ method.Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. TensorFlow Text contains collection of text related ...
Read MoreHow can Tensorflow be used to instantiate an estimator using Python?
An estimator can be instantiated using Tensorflow by using the ‘DNNClassifier’ method that is present in ‘estimator’ class of Tensorflow library.Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. TensorFlow Text contains collection ...
Read More