Pandas pivot_table()

1 개요[ | ]

Pandas pivot_table()

2 예시 1[ | ]

import pandas as pd
df = pd.DataFrame({
'id': [1,2,3,4,5,6,7,8,9,10,11,12,13,14],
'user_id': [1,1,1,1,1,2,2,2,2,3,3,3,3,3],
'product_id': [1,2,3,4,5,1,2,3,4,1,2,4,5,6],
'quantity': [1,1,2,10,5,4,10,3,1,2,1,1,1,1]
})
df
df.pivot_table(values='quantity', index='product_id', columns='user_id', fill_value=0)

3 예시 2[ | ]

import pandas as pd
import numpy as np
df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                         "bar", "bar", "bar", "bar"],
                   "B": ["one", "one", "one", "two", "two",
                         "one", "one", "two", "two"],
                   "C": ["small", "large", "large", "small",
                         "small", "large", "small", "small",
                         "large"],
                   "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
                   "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
df
table = pd.pivot_table(df, values='D', index=['A', 'B'],
                    columns=['C'], aggfunc=np.sum)
table
table = pd.pivot_table(df, values='D', index=['A', 'B'],
                    columns=['C'], aggfunc=np.sum, fill_value=0)
table
table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
                    aggfunc={'D': np.mean,
                             'E': np.mean})
table
table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
                    aggfunc={'D': np.mean,
                             'E': [min, max, np.mean]})
table

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}