728x90

Relative Keyword

  • dataframe row to columns
  • dataframe pivoting 
  • dataframe transpose
  • dataframe swap row and columns
import pandas as pd

#Create dataframe
salarydf = pd.DataFrame(data=[
        ['2022-01-25', 'John' , 20000],
        ['2022-01-25', 'Selly', 25000],
        ['2022-01-25', 'Tina' , 32000],
        ['2022-02-25', 'John' , 21000],
        ['2022-02-25', 'Selly', 24000],
        ['2022-02-25', 'Tina' , 31000],
        ['2022-03-25', 'John' , 21200],
        ['2022-03-25', 'Selly', 24500],
        ['2022-03-25', 'Tina' , 31500],
    ], columns=['date', 'employee', 'salary'])

#dataframe pivoting
salarydf.pivot(index="date", columns=["employee"], values=['salary'])

dataframe.pivot

# Dataframe.transpose : swap row and columns
salarydf.T

dataframe.T

 

pandas.DataFrame.pivot API Document

더보기

pandas.DataFrame.pivot

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

Return reshaped DataFrame organized by given index / column values.

Reshape data (produce a “pivot” table) based on column values. Uses unique values from specified index / columns to form axes of the resulting DataFrame. This function does not support data aggregation, multiple values will result in a MultiIndex in the columns. See the User Guide for more on reshaping.

 

Parameters

index : str or object or a list of str, optional

Column to use to make new frame’s index. If None, uses existing index.

Changed in version 1.1.0: Also accept list of index names.

columns : str or object or a list of str

Column to use to make new frame’s columns.

Changed in version 1.1.0: Also accept list of columns names.

values : str, object or a list of the previous, optional

Column(s) to use for populating new frame’s values. If not specified, all remaining columns will be used and the result will have hierarchically indexed columns.

 

Returns : DataFrame

Returns reshaped DataFrame.

 

RaisesValueError:

When there are any index, columns combinations with multiple values. DataFrame.pivot_table when you need to aggregate.

 

 

source : https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pivot.html#pandas.DataFrame.pivot

https://unsplash.com/photos/UxDU0Gg5pqQ

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기
반응형