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.transpose : swap row and columns
salarydf.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
'Data Science > Python' 카테고리의 다른 글
[python] Accelerate Requests Using asyncio (0) | 2022.09.01 |
---|---|
[pandas] Adding days to datetime dataframe column (0) | 2022.09.01 |
[Pandas] Database-style DataFrame joining/merging (0) | 2022.04.16 |
[pandas] Dataframe concatenate (0) | 2022.04.16 |
[Python] NumPy Tutorial #2 : shape & reshape (0) | 2022.04.12 |
최근댓글