Calculate Default Float Quantile Value in Python

Vani Nalliappan
Updated on 24-Feb-2021 09:11:38

123 Views

Input −Assume you have a series and default float quantilevalue is 3.0SolutionTo solve this, we will follow the steps given below −Define a SeriesAssign quantile default value .5 to the series and calculate the result. It is defined below,data.quantile(.5) ExampleLet us see the complete implementation to get a better understanding −import pandas as pd l = [10,20,30,40,50] data = pd.Series(l) print(data.quantile(.5))Output30.0

Count Records Based on Designation in a DataFrame Using Python

Vani Nalliappan
Updated on 24-Feb-2021 09:10:56

150 Views

Input −Assume, we have a DataFrame and group the records based on the designation is −Designation architect    1 programmer   2 scientist    2SolutionTo solve this, we will follow the below approaches.Define a DataFrameApply groupby method for Designation column and calculate the count as defined below,df.groupby(['Designation']).count()ExampleLet us see the following implementation to get a better understanding.import pandas as pd data = { 'Id':[1,2,3,4,5],          'Designation': ['architect','scientist','programmer','scientist','programmer']} df = pd.DataFrame(data) print("DataFrame is",df) print("groupby based on designation:") print(df.groupby(['Designation']).count())OutputDesignation architect    1 programmer   2 scientist    2

Store City and State Names Starting with K in CSV using Python

Vani Nalliappan
Updated on 24-Feb-2021 09:07:57

692 Views

Input −Assume, we have DataFrame with City and State columns and find the city, state name startswith ‘k’ and store into another CSV file as shown below −City, State Kochi, KeralaSolutionTo solve this, we will follow the steps given below.Define a DataFrameCheck the city starts with ‘k’ as defined below, df[df['City'].str.startswith('K') & df['State'].str.startswith('K')] Finally, store the data in the ‘CSV’ file as below, df1.to_csv(‘test.csv’)ExampleLet us see the following implementation to get a better understanding.import pandas as pd import random as r data = { 'City': ['Chennai', 'Kochi', 'Kolkata'], 'State': ['Tamilnad', 'Kerala', 'WestBengal']} df = pd.DataFrame(data) print("DataFrame is", df) df1 = ... Read More

Select Random Row from DataFrame in Python

Vani Nalliappan
Updated on 24-Feb-2021 09:05:45

445 Views

Input −Assume, sample DataFrame is,  Id Name 0 1 Adam 1 2 Michael 2 3 David 3 4 Jack 4 5 PeterOutputput −Random row is   Id    5 Name PeterSolutionTo solve this, we will follow the below approaches.Define a DataFrameCalculate the number of rows using df.shape[0] and assign to rows variable.set random_row value from randrange method as shown below.random_row = r.randrange(rows)Apply random_row inside iloc slicing to generate any random row in a DataFrame. It is defined below, df.iloc[random_row, :]ExampleLet us see the following implementation to get a better understanding.import pandas as pd import random as r data = { ... Read More

Sort DataFrame by Name Column in Descending Order using Python

Vani Nalliappan
Updated on 24-Feb-2021 07:21:58

645 Views

Input −Assume, sample DataFrame is,   Id Name 0 1 Adam 1 2 Michael 2 3 David 3 4 Jack 4 5 PeterOutput −After, sorting the elements in descending order as,   Id Name 4 5 Peter 1 2 Michael 3 4 Jack 2 3 David 0 1 AdamSolutionTo solve this, we will follow the below approaches.Define a DataFrameApply DataFrame sort_values method based on Name column and add argument ascending=False to show the data in descending order. It is defined below, df.sort_values(by='Name', ascending=False)ExampleLet us see the following implementation to get a better understanding.import pandas as pd data = {'Id': [1, ... Read More

Calculate Mean Product of DataFrame Values in Python

Vani Nalliappan
Updated on 24-Feb-2021 07:19:56

598 Views

Input −Assume, sample DataFrame is,  Id Age  salary 0 1 27   40000 1 2 22   25000 2 3 25   40000 3 4 23   35000 4 5 24   30000 5 6 32   30000 6 7 30   50000 7 8 28   20000 8 9 29   32000 9 10 27  23000Output −Result for mean and product of given slicing rows are, mean is Age          23.333333 salary    33333.333333 product is Age                12650 salary    35000000000000SolutionTo solve this, we will follow the below approaches.Define ... Read More

Count Ages Between 20 to 30 in a DataFrame using Python

Vani Nalliappan
Updated on 24-Feb-2021 07:15:41

1K+ Views

Input −Assume, you have a DataFrame, Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18Output −Total number of age between 20 to 30 is 2.SolutionTo solve this, we will follow the below approaches.Define a DataFrameSet the DataFrame Age column between 20,30. Store it in result DataFrame. It is defined below,df[df['Age'].between(20,30)]Finally, calculate the length of the result.ExampleLet us see the following implementation to get a better understanding.import pandas as pd data = {'Id':[1,2,3,4,5],'Age':[21,23,32,35,18]} df = pd.DataFrame(data) print(df) print("Count the age between 20 to 30") result = df[df['Age'].between(20,30)] print(len(result))Output Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18 Count the age between 20 to 30 2

Print A-Grade Students Names from DataFrame in Python

Vani Nalliappan
Updated on 24-Feb-2021 07:12:02

653 Views

Input −Assume, you have DataFrame,  Id  Name Grade 0 1 stud1   A 1 2 stud2   B 2 3 stud3   C 3 4 stud4   A 4 5 stud5   AOutput −And the result for ‘A’ grade students name, 0    stud1 3    stud4 4    stud5SolutionTo solve this, we will follow the below approaches.Define a DataFrameCompare the value to the DataFramedf[df['Grade']=='A']Store the result in another DataFrame and fetch Name.ExampleLet us see the following implementation to get a better understanding.import pandas as pd data = [[1, 'stud1', 'A'], [2, 'stud2', 'B'], [3, 'stud3', 'C'], [4, 'stud4', 'A'], ... Read More

Find Minimum Age of Employee in DataFrame using Python

Vani Nalliappan
Updated on 24-Feb-2021 07:04:25

965 Views

Input −Assume, you have a DataFrameDataFrame is  Id    Age   Salary 0 1    27    40000 1 2    22    25000 2 3    25    40000 3 4    23    35000 4 5    24    30000 5 6    32    30000 6 7    30    50000 7 8    28    20000 8 9    29    32000 9 10   27    23000Output −And, the result for a minimum age of an employee id and salary,  Id Salary 1 2 25000SolutionTo solve this, we will follow the below approaches.Define a DataFrameSet ... Read More

Find Maximum Length of a String in Python

Vani Nalliappan
Updated on 24-Feb-2021 07:02:12

572 Views

Input −Assume, we have a Series like this, [“one”, “two”, “eleven”, “pomegranates”, “three”] and the maximum length of the string is “Pomegranates”SolutionTo solve this, we will follow the below approaches.Define a SeriesSet the initial value of a maxlen is 0Set the “maxstr” value is initially empty string.Create a for loop and access all the values in the Series one by one and create an if condition to compare the value based on the length as follows −for i in res:    if(len(i)>maxlen):       maxlen = len(i)       maxstr = iFinally, print the value stored in the ... Read More

Advertisements