Found 507 Articles for Pandas

Write a program in Python to remove the elements in a series, if it contains exactly two spaces

Vani Nalliappan
Updated on 24-Feb-2021 06:31:30

153 Views

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 More

Write a program in Python to sort all the elements in a given series in descending order

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

118 Views

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

Write a program in Python to verify kth index element is either alphabet or number in a given series

Vani Nalliappan
Updated on 24-Feb-2021 06:26:46

96 Views

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 More

Write a program in Python to print the elements in a series between a specific range

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

733 Views

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]

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

517 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

82 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

489 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

213 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

171 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

How to select subsets of data In SQL Query Style in Pandas?

Kiran P
Updated on 10-Nov-2020 06:52:12

272 Views

IntroductionIn this post, I will show you how to perform Data Analysis with SQL style filtering with Pandas. Most of the corporate company’s data are stored in databases that require SQL to retrieve and manipulate it. For instance, there are companies like Oracle, IBM, Microsoft having their own databases with their own SQL implementations.Data scientists have to deal with SQL at some stage of their career as the data is not always stored in CSV files. I personally prefer to use Oracle, as the majority of my company’s data is stored in Oracle.Scenario – 1 Suppose we are given a ... Read More

Advertisements