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
- sqlite
- GIT
- node.js
- PyQt
- tensorflow
- mssql
- 날짜
- Unity
- flutter
- pandas
- 라즈베리파이
- port
- PyQt5
- 다이어트
- ASP
- swift
- MS-SQL
- ubuntu
- Linux
- 함수
- MySQL
- PER
- javascript
- urllib
- python
- 맛집
- Excel
- 리눅스
- 유니티
- IOS
Archives
아미(아름다운미소)
ES문자열 제거 본문
import pandas as pd
import numpy as np
# 테스트 데이터
data = {'a': ['TESTES', 'EXAMPLE', 'YESES', 'NO', pd.NA, 12345]}
df = pd.DataFrame(data)
# 벡터화된 연산으로 'ES' 제거 및 결측값 처리
df['a_cleaned'] = np.where(
df['a'].notna(), # 결측값이 아닌 경우에만 처리
df['a'].astype(str).str.replace(r'ES$', '', regex=True), # ES 제거
'' # 결측값은 빈 문자열로 처리
)
print(df)
import numpy as np
import pandas as pd
# 예시 데이터프레임 생성
df = pd.DataFrame({'a': ['ES123', 'ES456', None, 'ES789', '123ES', ''], 'bbb': [1, 2, None, 4, 5, None]})
# 조건과 결과를 정의
conditions = [
df['bbb'].isna(), # 'bbb' 열이 NaN인 경우
df['a'].isna(), # 'a' 열이 NaN인 경우
True # 그 외 경우
]
choices = [
'', # 'bbb' 열이 NaN인 경우
'', # 'a' 열이 NaN인 경우
df['a'].astype(str).str.replace(r'ES$', '', regex=True) # 'ES$' 패턴 제거
]
# np.select를 사용하여 조건에 따라 값을 할당
df['aaa'] = np.select(conditions, choices, default='')
print(df)
import numpy as np
import pandas as pd
# 예시 데이터프레임 생성
df = pd.DataFrame({'a': ['ES123', 'ES456', None, 'ES789', '123ES', ''], 'bbb': [1, 2, None, 4, 5, None]})
# 조건에 따라 처리
df['aaa'] = np.where(
df['bbb'].isna(), # 'bbb' 열이 NaN인 경우
'', # 빈 문자열로 설정
np.where(
df['a'].isna(), # 'a' 열이 NaN인 경우
'', # 빈 문자열로 설정
df['a'].astype(str).str.replace(r'ES$', '', regex=True) # 'ES$' 패턴 제거
)
)
print(df)
import pandas as pd
import re
# 테스트 데이터
data = {'a': ['TESTES', 'EXAMPLE', 'YESES', 'NO', pd.NA, 12345]}
df = pd.DataFrame(data)
# 정규표현식 컴파일
pattern = re.compile(r'ES$')
# 벡터화된 연산 + 결측값 처리
df['a_cleaned'] = df['a'].fillna('').astype(str).str.replace(pattern, '', regex=True)
print(df)
import pandas as pd
import numpy as np
# 테스트 데이터
data = {'a': ['TESTES', 'EXAMPLE', 'YESES', 'NO', np.nan, 12345]}
df = pd.DataFrame(data)
# 벡터화된 연산으로 'ES' 제거
df['a_cleaned'] = df['a'].astype(str).str.replace(r'ES$', '', regex=True)
# NaN 값을 빈 문자열로 대체 (필요한 경우)
df['a_cleaned'] = df['a_cleaned'].replace('nan', '')
print(df)
'랭귀지 > pandas' 카테고리의 다른 글
pandas의 str.split(expand=True)를 사용한 방법 (0) | 2025.03.17 |
---|---|
두 csv 비교 (0) | 2025.03.14 |
splite (0) | 2025.03.13 |
날짜 형식 변환 (YYYY-MM-DD 형식으로) (0) | 2025.03.12 |
백터화 (0) | 2025.03.11 |
Comments