inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

핵심만 빠르게, 입문자를 위한 파이썬(Python)과 판다스(Pandas)

Pandas의 연산 기능 (sum, divide, cumulative)

divide 함수 축 질문

69

SB

작성한 질문수 2

0

안녕하세요! pandas 연산기능 강의에서 이해가 가지 않는 부분이 있어 질문 드립니다.

df.divide(df.sum(axis=1), axis=0)

위 코드에서 열 축을 따라 왼쪽에서 오른쪽으로 나눗셈이 이루어지는데, 왜 axis=0 인가요? sum과 동일한 방향이면 똑같이 axis=1 이어야 할 것 같은데.. 헷갈립니다.

python pandas

답변 1

0

인프런 AI 인턴

안녕하세요, 인프런 AI 인턴입니다. Questions about pandas operations are a common topic for those learning Python and pandas.

The divide function in pandas, when used as df.divide(df.sum(axis=1), axis=0), can indeed be a bit confusing at first glance. Here’s an explanation:

  1. df.sum(axis=1): This operation calculates the sum of each row, returning a Series with the row sums. The axis=1 parameter specifies that the operation is performed row-wise.

  2. df.divide(..., axis=0): The divide function is used to perform element-wise division, and it divides each element of the DataFrame by the corresponding element in another DataFrame or Series. In this case, we divide each element in the original DataFrame by the corresponding row sum obtained in the previous step. Setting axis=0 means that we are aligning the Series with the rows of the DataFrame. Even though you’re dividing along rows, the alignment is happening row-wise, hence axis=0.

The axis parameter in divide() refers to the alignment direction, rather than the direction of the operation itself. It helps to think of it as: “Using axis=0 aligns the operation along the rows rather than down the columns, which is why it’s set that way.”

For more insight, you can check out similar discussions in these posts:

Both links detail axes manipulations with pandas, specifically through examples with the apply function, which might be helpful to understand this concept.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.

codex에게 대용량 코드베이스를 인식 시키는 방법은 어떤게 있나요?

0

2

0

출력값 질문

0

9

1

슬롯 관련 문의

1

10

2

수업노트가 어디에 있나요?

0

19

1

[긴급요청] 28강 동영상 전체에 오디오가 잘못들어가있습니다.

0

16

1

실제 계좌 잔고 및 테스트중에

1

29

4

Replit UI 변경으로 인한 실습 진행 문의

1

16

1

29강 5:00

0

23

2

설치 및 설정 가이드 노션 자료는 없나요 ?

0

22

2

실기시험 제출관련

0

152

2

6.20 작업형 2 과적합

0

157

3

install까지 설치 하였는데 start 가안됩니다.

1

26

1

8분54초 테이블 내용 문의

0

22

1

코딩팡 장업형2 베이스 라인 인코딩 종류 질문

0

49

2

multi_tool_agent.py 에서 arxiv tool 에러

0

18

1

로지스틱회귀, 회귀

0

48

2

가상 환경과 차이 도커 질문

0

24

1

자료 다운로드 위치 확인 부탁드립니다.

0

15

1

파이썬 연습문제 6번 관련 문의

0

75

1

쥬피터노트북이 안깔려요

0

461

1

pandas 연습문제 4번 관련

0

357

1

Pandas 연습문제 4. apply 활용 질문

0

571

1

마크다운 blank line

0

822

1

내국인 생활인구.csv 다운위치

0

604

1