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 |
Tags
- ASP
- 리눅스
- PER
- Linux
- 다이어트
- mssql
- PyQt5
- ubuntu
- GIT
- flutter
- Excel
- port
- PyQt
- 라즈베리파이
- 날짜
- 함수
- sqlite
- Unity
- swift
- javascript
- 맛집
- MySQL
- 유니티
- node.js
- urllib
- pandas
- MS-SQL
- python
- tensorflow
- IOS
Archives
아미(아름다운미소)
np.select 본문
import pandas as pd
import numpy as np
# 샘플 DataFrame 생성
df = pd.DataFrame({
"fruit": ["apple", "banana", "cherry", "date", "elderberry"],
"quantity": [5, 3, 7, 2, 4],
"price": [100, 50, 200, 30, 150]
})
# 조건 리스트 (여러 컬럼을 함께 비교)
condlist = [
(df["fruit"] == "apple") & (df["quantity"] > 4), # "apple"이고 quantity가 4보다 큰 경우
(df["fruit"] == "banana") | (df["price"] < 100), # "banana"이거나 price가 100보다 작은 경우
(df["price"] >= 150) # price가 150 이상인 경우
]
# 선택할 값 리스트
choicelist = [1, 2, 3]
# np.select 사용 (조건에 맞지 않는 경우 default 값은 0)
df["new_column"] = np.select(condlist, choicelist, default=0)
print(df)
'랭귀지 > pandas' 카테고리의 다른 글
아래는 두 개의 동일한 구조를 가진 데이터프레임을 생성하고, 두 데이터프레임의 값을 비교하여 **틀린 행을 찾아내는 예제** (0) | 2025.02.18 |
---|---|
Pandas에서 특정 조건을 만족하지 않을 경우, 날짜 열을 비교하여 한 주 전의 값을 가져오기 (0) | 2025.02.18 |
bool 결측치 처리 (0) | 2025.02.04 |
assign (0) | 2025.01.09 |
pandas 빈값이 아닌면 a컬럼을 _기준으로 split 하고 첫번째값 사용 (0) | 2025.01.03 |
Comments