Basic knowledge points of pivot of function in python

  • 2021-09-05 00:30:08
  • OfStack

Different from the function usage introduced to you in the past, The way we can use pivot function is to reshape the use of data, which is not common in the use of python. However, if we need to use this function, we can basically choose the function to call. pivot function 1 is definitely on the list. Let's give you a detailed explanation around this function. Let's take a look at it.

Function syntax:


pivot()

Parameters:

Index and columns should be noted that the former is an optional parameter and the latter is a required parameter.

Use example:


import pandas as pd
df=pd.read_csv("user_label_part1.csv",sep=',',encoding='gbk')

Note:

When analyzing data, remember to put pivot results reset_index ()

Function uses:


df.head(10)
temp=df.pivot(index='user_log_acct',columns='item_third_cate_cd',values='label')
temp.to_csv("res.csv",sep=',',encoding='gbk')
df.head(3)

Explanation of pivot Function Structure of Python


pivot(index=None, columns=None, values=None)

index: Optional parameter. Sets the row index of the new dataframe, and if not specified, uses the currently existing row index.

columns: Required. Used to set the column index as the new dataframe.

values: Optional parameter. Select the value of one column/several columns in the original dataframe to display it in the column of the new dataframe. If not specified, all the columns in the original dataframe will be displayed by default. Note here that in order to display all the values, there will be a multi-layer row index.

The above is the pivot () function in python basic knowledge points of the details, more about python pivot () function is what information please pay attention to other related articles on this site!


Related articles: