일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Splunk
- 알고리즘
- Lotto
- pip
- javascript
- DFS
- Numpy
- synology
- pycharm
- LSTM
- imread
- Python
- GitHub
- mean
- install
- keras
- GT-S80
- dataframe
- E-P1
- Button
- index
- SPL
- SciPy
- CNN
- ipad
- pandas
- RNN
- Series
- mariadb
- 삼성소프트웨어멤버십
Archives
- Today
- Total
잠토의 잠망경
[Pandas] Groupby 본문
Groupby 사용
하기 csv에서 col1을 기준으로 col3의 평균을 구하자
df.groupby(대상)
col1,col2,col3,col4
1,2,1,4
1,1,2,3
1,1,3,4
2,0,4,3
2,2,5,4
3,1,6,3
3,2,7,4
import pandas as pd
df = pd.read_csv('input.csv')
print(df.groupby('col1')['col3'].mean())
출력
col1
1 2.0
2 4.5
3 6.5
Name: col3, dtype: float64
구조 설명
groupby: DataFrame
1개열 : Series
groupbyed = df.groupby('col1')
print(type(groupbyed)) # DataFrameGroupBy
groupbyed_target = groupbyed['col3']
print(type(groupbyed_target)) # SeriesGroupBy
여러 인자로 Groupby
import pandas as pd
df = pd.read_csv('input.csv')
print(df)
groupbyed = df.groupby(['col1','col2'])[['col3', 'col4']].mean()
print(groupbyed)
print(type(groupbyed)) # dataframe
output
col3 col4
col1 col2
1 1 2.5 3.5
2 1.0 4.0
2 0 4.0 3.0
2 5.0 4.0
3 1 6.0 3.0
2 7.0 4.0
>>> print(type(groupbyed))
<class 'pandas.core.frame.DataFrame'>
그룹화한 데이터 개수(빈도수)
import pandas as pd
df = pd.read_csv('input.csv')
print(df)
groupbyed = df.groupby(['col1'])['col3'].nunique()
print(groupbyed)
print(type(groupbyed))
col1
1 3
2 2
3 2
Comments