Notice
														
												
											
												
												
													Recent Posts
													
											
												
												
													Recent Comments
													
											
												
												
													Link
													
											
									| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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
													
											
												
												- 유니티
- 맛집
- mssql
- swift
- MS-SQL
- PyQt
- python
- PER
- 리눅스
- tensorflow
- 라즈베리파이
- ASP
- Linux
- flutter
- urllib
- port
- Unity
- pandas
- MySQL
- 날짜
- ubuntu
- PyQt5
- javascript
- IOS
- 다이어트
- node.js
- Excel
- GIT
- 함수
- sqlite
													Archives
													
											
											
											
											아미(아름다운미소)
pandas groupby sum fillna astype 본문
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank'],
    'Department': ['Sales', 'Sales', 'IT', 'IT', 'HR', 'HR'],
    'Score': ['100', '200', '300', '400', '500', None]
}
df = pd.DataFrame(data)
# 데이터 타입을 int로 변환하고, None 값을 0으로 채우기
df['Score'] = df['Score'].astype(int).fillna(0)
grouped = df.groupby(['Department'])['Score'].sum().reset_index()
print(grouped)
  Department  Score
0        HR   1000
1         IT   1000
2      Sales   1000sum column1개
import pandas as pd
data = {'A': ['a', 'b', 'c', 'a', 'b'], 
        'B': ['a', 'b', 'c', 'a', 'b'],
        'C1': ['10', '20', '30', 'abc', '50']}
df = pd.DataFrame(data)
# 결측치 처리
df['C1'] = df['C1'].fillna(0).astype(int)
# groupby 수행
grouped = df.groupby(['A', 'B'])
result = grouped['C1'].sum().reset_index()
print(result)sum column 복수
import pandas as pd
data = {'A': ['a', 'b', 'c', 'a', 'b'], 
        'B': ['a', 'b', 'c', 'a', 'b'],
        'C1': ['10', '20', '30', 'abc', '50'],
        'C2': [1, 2, 3, 'def', 5]}
df = pd.DataFrame(data)
# 결측치 처리
df['C1'] = df['C1'].fillna(0).astype(int)
df['C2'] = df['C2'].fillna(0).astype(int)
# groupby 수행
grouped = df.groupby(['A', 'B'])
result = grouped[['C1', 'C2']].sum().reset_index()
print(result)'랭귀지 > pandas' 카테고리의 다른 글
| pandas concat option (0) | 2024.07.13 | 
|---|---|
| pandas concat (0) | 2024.07.13 | 
| pandas skipna (0) | 2024.07.13 | 
| pandas groupby sum (0) | 2024.07.12 | 
| pandas index 확인 (0) | 2024.07.11 | 
			  Comments