공부
[python] pandas dataframe filter str.startswith
승가비
2023. 4. 9. 13:04
728x90
import pandas as pd
# 예시 데이터프레임 생성
df = pd.DataFrame({'이름': ['홍길동', '김철수', '이영희', '박종민', '정지원'],
'나이': [30, 25, 28, 35, 22],
'성별': ['남', '남', '여', '남', '남']})
# '이름' 컬럼에서 성이 '김'인 경우만 필터링하여 새로운 데이터프레임 생성
filtered_df = df[df['이름'].str.startswith('김')]
# 필터링된 결과 출력
print(filtered_df)
reference: chatGPT
728x90