Quantify the Shape of a Distribution in a DataFrame using Python

Vani Nalliappan
Updated on 25-Feb-2021 05:44:50

307 Views

Assume, you have a dataframe and the result for quantify shape of a distribution is, kurtosis is: Column1    -1.526243 Column2     1.948382 dtype: float64 asymmetry distribution - skewness is: Column1    -0.280389 Column2     1.309355 dtype: float64SolutionTo solve this, we will follow the steps given below −Define a dataframeApply df.kurt(axis=0) to calculate the shape of distribution, df.kurt(axis=0)Apply df.skew(axis=0) to calculate unbiased skew over axis-0 to find asymmetry distribution, df.skew(axis=0)ExampleLet’s see the following code to get a better understanding −import pandas as pd data = {"Column1":[12, 34, 56, 78, 90],          "Column2":[23, 30, 45, ... Read More

Find Mean Absolute Deviation of Rows and Columns in a DataFrame using Python

Vani Nalliappan
Updated on 25-Feb-2021 05:42:20

514 Views

SolutionAssume you have a dataframe and mean absolute deviation of rows and column is, mad of columns: Column1    0.938776 Column2    0.600000 dtype: float64 mad of rows: 0    0.500 1    0.900 2    0.650 3    0.900 4    0.750 5    0.575 6    1.325 dtype: float64To solve this, we will follow the steps given below −Define a dataframeCalculate mean absolute deviation of row as, df.mad()Calculate mean absolute deviation of row as, df.mad(axis=1)ExampleLet’s see the following code to get a better understanding −import pandas as pd data = {"Column1":[6, 5.3, 5.9, 7.8, 7.6, 7.45, 7.75], ... Read More

Find the Average of First Row in a Panel Using Python

Vani Nalliappan
Updated on 25-Feb-2021 05:37:27

287 Views

Assume, you have Panel and the average of the first row is, Average of first row is: Column1    0.274124 dtype: float64SolutionTo solve this, we will follow the steps given below −Set data value as dictionary key is ‘Column1’ with value as pd.DataFrame(np.random.randn(5, 3))data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))}Assign data to Panel and save it as pp = pd.Panel(data)Print the column using dict key Column1print(p['Column1'])Calculate theAverage of first row using, major_xs(0) ,p.major_xs(0).mean()ExampleLet’s see the following code to get a better understanding −import pandas as pd import numpy as np data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))} p = pd.Panel(data) print("Panel values:") ... Read More

Find Minimum Rank of a Column in DataFrame using Python

Vani Nalliappan
Updated on 25-Feb-2021 05:33:26

353 Views

SolutionAssume, you have a dataframe and minimum rank of a particular column,  Id Name    Age    Rank 0 1 Adam    12    1.0 1 2 David   13    3.0 2 3 Michael 14    5.0 3 4 Peter   12    1.0 4 5 William 13    3.0To solve this, we will follow the steps given below −Define a dataframe.Assign df[‘Age’] column inside rank function to calculate the minimum rank for axis 0 is, df["Age"].rank(axis=0, method ='min', ascending=True)ExampleLet’s see the following code to get a better understanding −import pandas as pd data = {'Id': [1, 2, 3, ... Read More

Create a Panel from Dictionary of DataFrame in Python

Vani Nalliappan
Updated on 25-Feb-2021 05:32:24

196 Views

The result for a maximum value of the first column in panel ismaximum value of first column is ; Column1    1.377292SolutionTo solve this, we will follow the below approach −Set data value as dictionary key is ‘Column1’ with value as pd.DataFrame(np.random.randn(5, 3))data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))}Assign data to Panel and save it as pp = pd.Panel(data)Print the column using dict key Column1print(p['Column1'])Calculate the maximum value of first column using, minor_xs(0) ,p.minor_xs(0).max()ExampleLet’s see the following code to get a better understanding −import pandas as pd import numpy as np data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))} p = pd.Panel(data) print("Panel ... Read More

Shift DataFrame Index by Two Periods in Python

Vani Nalliappan
Updated on 25-Feb-2021 05:29:56

171 Views

Assume, you have a dataframe and the shift index by two periods in positive and negative direction is, shift the index by three periods in positive direction                      Id Age 2020-01-01 00:00:00 NaN NaN 2020-01-01 12:00:00 NaN NaN 2020-01-02 00:00:00 1.0 10.0 2020-01-02 12:00:00 2.0 12.0 2020-01-03 00:00:00 3.0 14.0 shift the index by three periods in negative direction                      Id Age 2020-01-01 00:00:00 3.0 14.0 2020-01-01 12:00:00 4.0 11.0 2020-01-02 00:00:00 5.0 13.0 2020-01-02 12:00:00 NaN NaN 2020-01-03 00:00:00 NaN NaNSolutionTo ... Read More

Remove First Duplicate Rows in a DataFrame using Python

Vani Nalliappan
Updated on 25-Feb-2021 05:28:07

300 Views

Assume, you have a dataframe and the result for removing first duplicate rows are,     Id Age 0    1 12 3    4 13 4    5 14 5    6 12 6    2 13 7    7 16 8    3 14 9    9 15 10  10 14SolutionTo solve this, we will follow the steps given below −Define a dataframeApply drop_duplicates function inside Id and Age column then assign keep initial value as ‘last’.df.drop_duplicates(subset=['Id', 'Age'], keep='last')Store the result inside same dataframe and print itExampleLet’s see the below implementation to get a better understanding −import pandas ... Read More

Compute Grouped Data Covariance in Python

Vani Nalliappan
Updated on 25-Feb-2021 05:26:51

383 Views

Assume, you have a dataframe and the result for calculating covariance from grouped data and corresponding column as, Grouped data covariance is:                   mark1       mark2 subjects maths    mark1    25.0    12.500000          mark2    12.5    108.333333 science  mark1    28.0    50.000000          mark2    50.0    233.333333 Grouped data covariance between two columns: subjects maths    12.5 science  50.0 dtype: float64SolutionTo solve this, we will follow the steps given below −Define a dataframeApply groupby function inside dataframe subjects ... Read More

Reshape DataFrame in Different Ways Using Python

Vani Nalliappan
Updated on 25-Feb-2021 05:24:37

462 Views

We can reshape a dataframe using melt(), stack(), unstack() and pivot() function.Solution 1Define a dataframe.Apply melt() function to convert wide dataframe column as rows. It is defined below, df.melt()ExampleLet’s see the below code to get a better understanding −import pandas as pd df = pd.DataFrame({'Id':[1, 2, 3], 'Age':[13, 14, 13], 'Mark':[80, 90, 85]}) print("Dataframe is:", df) print(df.melt())OutputDataframe is:  Id Age Mark 0 1 13   80 1 2 14   90 2 3 13   85 variable value 0    Id    1 1    Id    2 2    Id    3 3   Age    13 4   ... Read More

Sum Excluding One Element in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:38:37

738 Views

Suppose we have an array of Integers like this −const arr = [12, 1, 4, 8, 5];We are required to write a JavaScript function that takes in one such array as the only argument.The function should then return an array of exactly two integers −First integer should be the smallest possible sum of all the array elements excluding any one element.Second integer should be the greatest possible sum of all the array elements excluding any one element.The only condition for us is that we have to do this using one and only one for loop.For example −For the above array, ... Read More

Advertisements