Found 507 Articles for Pandas

Write a Python code to find a cross tabulation of two dataframes

Vani Nalliappan
Updated on 25-Feb-2021 05:59:10

443 Views

Assume you have two dataframes and the result for cross-tabulation is,Age  12 13 14 Mark 80 90 85 Id 1    1  0  0 2    0  1  0 3    1  0  0 4    0  1  0 5    0  0  1SolutionTo solve this, we will follow the steps given below −Define two dataframesApply df.crosstab() function inside index as ‘Id’ and columns as ‘Age’ and ‘Mark’. It is defined below,pd.crosstab(index=df['Id'],columns=[df['Age'],df1['Mark']])Exampleimport pandas as pd df = pd.DataFrame({'Id':[1,2,3,4,5],'Age':[12,13,12,13,14]}) df1 = pd.DataFrame({'Mark':[80,90,80,90,85]}) print(pd.crosstab(index=df['Id'],columns=[df['Age'],df1['Mark']]))OutputAge  12 13 14 Mark 80 90 85 Id 1    1  0  0 2    0  1  0 3    1  0  0 4    0  1  0 5    0  0  1

Write a program in Python to print the length of elements in all column in a dataframe using applymap

Vani Nalliappan
Updated on 25-Feb-2021 05:58:11

268 Views

The result for the length of elements in all column in a dataframe is, Dataframe is:    Fruits    City 0 Apple    Shimla 1 Orange   Sydney 2 Mango    Lucknow 3 Kiwi    Wellington Length of the elements in all columns    Fruits City 0    5    6 1    6    6 2    5    7 3    4    10SolutionTo solve this, we will follow the steps given below −Define a dataframeUse df.applymap function inside lambda function to calculate the length of elements in all column asdf.applymap(lambda x:len(str(x)))ExampleLet’s check the following code to get ... Read More

Write a Python code to calculate percentage change between Id and Age columns of the top 2 and bottom 2 values

Vani Nalliappan
Updated on 25-Feb-2021 05:55:54

269 Views

Assume, you have dataframe and the result for percentage change between Id and Age columns top 2 and bottom 2 valueId and Age-top 2 values    Id Age 0 NaN NaN 1 1.0 0.0 Id and Age-bottom 2 values       Id      Age 3 0.000000 -0.071429 4 0.666667 0.000000SolutionTo solve this, we will follow the steps given below −Define a dataframeApply df[[‘Id’, ’Age’]].pct_change() inside slicing [0:2]df[['Id', 'Age']].pct_change()[0:2]Apply df[[‘Id’, ’Age’]].pct_change() inside slicing [-2:]df[['Id', 'Age']].pct_change()[0:2]ExampleLet’s check the following code to get a better understanding −import pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, None, 5],         ... Read More

Write a Python program to perform table-wise pipe function in a dataframe

Vani Nalliappan
Updated on 25-Feb-2021 05:48:54

128 Views

Assume, you have a dataframe and the result for table-wise function is, Table wise function:    Id  Mark 0  6.0 85.0 1  7.0 95.0 2  8.0 75.0 3  9.0 90.0 4 10.0 95.0SolutionTo solve this, we will follow the steps given below −Define a dataframeCreate a user-defined function avg with two arguments and return the result as (a+b/2). It is defined below, def avg(a, b):    return (a+b/2)Apply pipe() function to perform table-wise function inside first value as avg() and the second argument as 10 to calculate the avg of all the dataframe values.df.pipe(avg, 10)ExampleLet’s check the following code to ... Read More

Write a Python program to trim the minimum and maximum threshold value in a dataframe

Vani Nalliappan
Updated on 25-Feb-2021 05:46:06

335 Views

Assume, you have a dataframe and the result for trim of minimum and the maximum threshold value, minimum threshold:    Column1 Column2 0    30    30 1    34    30 2    56    30 3    78    50 4    30    90 maximum threshold:    Column1 Column2 0    12    23 1    34    30 2    50    25 3    50    50 4    28    50 clipped dataframe is:    Column1 Column2 0    30    30 1    34    30 2    50    30 3   ... Read More

Write a Python program to quantify the shape of a distribution in a dataframe

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

218 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

Write a Python program to find the mean absolute deviation of rows and columns in a dataframe

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

346 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

Write a Python program to find the average of first row in a Panel

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

198 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

Write a program in Python to find the minimum rank of a particular column in a dataframe

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

292 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

Write a program in Python to create a panel from a dictionary of dataframe and print the maximum value of the first column

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

121 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

Advertisements