데이터프레임을 사용해 코드를 작성하다보면 각 row를 순회하는 로직을 되게 자주 사용한다.
그런데 할 때마다 o_o ...? 코드가 바로 나오지 않고 멍 때리게 되어서 메모해둡니다
1. iterrows()
for index, row in df.iterrows():
print(f'Index: {index}, A: {row["A"]}, B: {row["B"]}')
2. itertuples()
for row in df.itertuples(index=False):
print(f'A: {row.A}, B: {row.B}')
3. apply()
def process_row(row):
return f'A: {row["A"]}, B: {row["B"]}'
df['Result'] = df.apply(process_row, axis=1)
print(df['Result'])
728x90
'데이터 엔지니어링 > Python' 카테고리의 다른 글
[OpenCV] 이미지 분석 시작하기 - 창 관리, 그레이스케일 (1) | 2024.02.29 |
---|---|
[Pandas] Dataframe row apply 활용법 (0) | 2024.01.15 |
[Pandas] FutureWarning: iteritems is deprecated and will be removed in a future version. Use .items instead. for column, series in pdf.iteritems() 오류 해결 - Pandas 버전 맞추기 (0) | 2023.02.24 |
[Python] classmethod vs staticmethod (0) | 2023.02.22 |
[Python] 도로명 주소 위도,경도로 전환하기 (지오코딩) (0) | 2022.11.02 |