Programming Articles - Page 1408 of 3363

Write a Python code to rename the given axis in a dataframe

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

307 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

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

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

548 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

391 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

422 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

241 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

How to change the legend shape using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 12:17:30

1K+ Views

By default, the shape of legend is circular but we can change it by using the guides function of ggplot2 package. For example, if we have a data frame with two numerical columns say x and y, and one categorical column Group then the scatterplot between x and y for different color values of categories in categorical column Group having different shape of legends can be created by using the below command −ggplot(df, aes(x, y, color=Group))+geom_point()+guides(colour=guide_legend(override.aes=list(shape=0)))Here, we can change the shape argument value to any value between starting from 0 to 25.Consider the below data frame −Example Live DemoxRead More

How to deal with error “undefined columns selected” while subsetting data in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 12:14:37

2K+ Views

When we do subsetting with the help of single square brackets we need to be careful about putting the commas at appropriate places. If we want to subset rows using the columns then comma needs to be placed before the condition. The “undefined columns selected” error occurs when we do not specify any comma. Check out the examples to understand how it works.Consider the below data frame −Example Live Demox15),]Output   x1 x2 1  7  0 2  6  4 4  6  1 7  6  1 9  7  3 11 6  3 12 9  2 15 7  4 16 7  3 17 6  2 18 6  3Example Live Demoy1

How to collapse data frame rows in R by summing using dplyr?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 12:05:26

3K+ Views

To collapse data frame rows by summing using dplyr package, we can use summarise_all function of dplyr package. For example, if we have a data frame called df that has a categorical column say Group and one numerical column then collapsing of rows by summing can be done by using the command −df%>%group_by(Group)%>%summarise_all(funs(sum))Consider the below data frame −Example Live DemoGroup

How to create a subset using character column with multiple matches in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 12:02:55

739 Views

Subsetting is one of the most important aspects of data analysis. One such situation could be subsetting the character column based on multiple values. For example, if a character column of an R data frame has 5 categories then we might want to extract only 2 or 3 or 4 values then it can be done by using the filter function of dplyr package with str_detect function of stringr package.Consider the below data frame −Example Live DemoGroup

How to find the frequency vector elements that exists in another vector in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:59:10

230 Views

If a vector value exists in another vector then we might want to find the frequency/count for such values in the other vector. For example, if we have two vectors say x and y, and some of the values in y exists in x as well. Therefore, we can find the frequency of values in x for y values can be found by using the command colSums(outer(x,y,"==")).Example Live Demox1

Advertisements