일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- LSTM
- RNN
- Python
- GT-S80
- E-P1
- ipad
- CNN
- Lotto
- Button
- SciPy
- 알고리즘
- SPL
- DFS
- index
- Series
- pycharm
- dataframe
- pip
- javascript
- Numpy
- mean
- GitHub
- pandas
- Splunk
- keras
- imread
- install
- synology
- mariadb
- 삼성소프트웨어멤버십
Archives
- Today
- Total
잠토의 잠망경
[pandas] groupby mulicolumns 본문
multi Column을 활용하여 Groupby하기
'''
dataframe을 이용하여 group by 진행할때 multicalumn을 이용하는 방법
'''
def makeGroup_s002()->None:
from numpy import ndarray
import numpy as np
from pandas import DataFrame
import pandas as pd
## Data 준비
datas = pd.DataFrame({'key1':['a', 'a', 'b', 'b', 'a'],
'key2':['one', 'two', 'one', 'two', 'one'],
'data1': np.random.rand(5),
'data2': np.random.rand(5)})
print(datas)
grouped:DataFrameGroupBy = datas.groupby(['key1', 'key2'])
print('-'*100)
for key, group in grouped:
print('group key: {0}'.format(key))
print(group)
print('-'*100)
group key: ('a', 'one')
key1 key2 data1 data2
0 a one 0.342689 0.819064
4 a one 0.161171 0.175721
----------------------------------------------------------------------------------------------------
group key: ('a', 'two')
key1 key2 data1 data2
1 a two 0.976412 0.727764
----------------------------------------------------------------------------------------------------
group key: ('b', 'one')
key1 key2 data1 data2
2 b one 0.497101 0.287573
----------------------------------------------------------------------------------------------------
group key: ('b', 'two')
key1 key2 data1 data2
3 b two 0.758967 0.617483
----------------------------------------------------------------------------------------------------
Process finished with exit code 0
Comments