Flatten Records in a DataFrame by C and F Order in Python

Vani Nalliappan
Updated on 25-Feb-2021 06:06:13

153 Views

Assume, you have a dataframe and the result for flatten records in C and F order as, flat c_order:    [10 12 25 13 3 12 11 14 24 15 6 14] flat F_order:    [10 25 3 11 24 6 12 13 12 14 15 14]SolutionTo solve this, we will follow the steps given below −Define a dataframeApply df.values.ravel() function inside set an argument as order=’C’ and save it as C_order, C_order = df.values.ravel(order='C')Apply df.values.ravel() function inside set an argument as order=’F’ and save it as F_order, F_order = df.values.ravel(order='F')ExampleLet’s check the following code to get a better understanding ... Read More

Print DataFrame Rows as OrderedDict in Python

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

115 Views

Assume, you have a dataframe and the result for orderDict with list of tuples are −OrderedDict([('Index', 0), ('Name', 'Raj'), ('Age', 13), ('City', 'Chennai'), ('Mark', 80)]) OrderedDict([('Index', 1), ('Name', 'Ravi'), ('Age', 12), ('City', 'Delhi'), ('Mark', 90)]) OrderedDict([('Index', 2), ('Name', 'Ram'), ('Age', 13), ('City', 'Chennai'), ('Mark', 95)])SolutionTo solve this, we will follow the steps given below −Define a dataframeSet for loop to access all the rows using df.itertuples() function inside set name=’stud’for row in df.itertuples(name='stud')Convert all the rows to orderDict with list of tuples using rows._asdict() function and save it as dict_row. Finally print the values, dict_row = row._asdict() print(dict_row)ExampleLet’s check the ... Read More

Calculate Adjusted and Non-Adjusted EWM in Python

Vani Nalliappan
Updated on 25-Feb-2021 06:03:40

186 Views

Assume, you have a dataframe and the result for adjusted and non-adjusted EWM are −adjusted ewm:       Id       Age 0 1.000000 12.000000 1 1.750000 12.750000 2 2.615385 12.230769 3 2.615385 13.425000 4 4.670213 14.479339 non adjusted ewm:       Id       Age 0 1.000000 12.000000 1 1.666667 12.666667 2 2.555556 12.222222 3 2.555556 13.407407 4 4.650794 14.469136SolutionTo solve this, we will follow the steps given below −Define a dataframeCalculate adjusted ewm with delay 0.5 using df.ewm(com=0.5).mean().df.ewm(com=0.5).mean()Calculate non-adjusted ewm with delay 0.5 using df.ewm(com=0.5).mean().df.ewm(com=0.5, adjust=False).mean()Exampleimport numpy as np import pandas as pd df ... Read More

Fill Missing Values in DataFrame using Python

Vani Nalliappan
Updated on 25-Feb-2021 06:02:17

272 Views

SolutionTo solve this, we will follow the steps given below −Define a dataframeApply df.interpolate funtion inside method =’linear’, limit_direction =’forward’ and fill NaN limit = 2df.interpolate(method ='linear', limit_direction ='forward', limit = 2Exampleimport pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, None, 5],                      "Age":[12, 12, 14, 13, None],                      "Mark":[80, 90, None, 95, 85],                   }) print("Dataframe is:",df) print("Interpolate missing values:") print(df.interpolate(method ='linear', limit_direction ='forward', limit = 2))OutputDataframe is:    Id     Age   Mark 0 1.0    12.0   80.0 1 2.0    12.0   90.0 2 3.0    14.0   NaN 3 NaN    13.0   95.0 4 5.0    NaN    85.0 Interpolate missing values:    Id     Age    Mark 0 1.0    12.0    80.0 1 2.0    12.0    90.0 2 3.0    14.0    92.5 3 4.0    13.0    95.0 4 5.0    13.0    85.0

Rename Axes in a DataFrame using Python

Vani Nalliappan
Updated on 25-Feb-2021 06:00:35

271 Views

Assume, you have a dataframe and the result for renaming the axis is,Rename index: index    Id    Age    Mark    0    1.0    12.0   80.0    1    2.0    12.0   90.0    2    3.0    14.0   NaN    3    NaN    13.0   95.0    4    5.0    NaN    85.0SolutionTo solve this, we will follow the steps given below −Define a dataframeApply df.rename_axis() function inside axis name as ‘index’ and set axis=1df.rename_axis('index',axis=1)Exampleimport pandas as pd df = pd.DataFrame({"Id":[1, 2, 3, None, 5],                      "Age":[12, 12, 14, 13, None],                      "Mark":[80, 90, None, 95, 85],                   }) print("Dataframe is:",df) print("Rename index:") df = df.rename_axis('index',axis=1) print(df)OutputDataframe is:    Id    Age    Mark 0 1.0    12.0   80.0 1 2.0    12.0   90.0 2 3.0    14.0   NaN 3 NaN    13.0   95.0 4 5.0    NaN    85.0 Rename index: index    Id    Age    Mark    0    1.0    12.0   80.0    1    2.0    12.0   90.0    2    3.0    14.0   NaN    3    NaN    13.0   95.0    4    5.0    NaN    85.0

Find Cross Tabulation of Two DataFrames in Python

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

524 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

Print Length of Elements in DataFrame using applymap in Python

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

372 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

Calculate Percentage Change Between ID and Age Columns in Python

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

392 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

Perform Table-wise Pipe Function in a DataFrame using Python

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

208 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

Trim Minimum and Maximum Threshold Value in a DataFrame using Python

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

445 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

Advertisements