랭귀지/pandas
pandas 몫 나머지
유키공
2024. 8. 13. 10:37
import pandas as pd
# 예시 데이터프레임 생성
data = {'수치': [-10, 20, -15, 30, -25]}
df = pd.DataFrame(data)
# 몫과 나머지를 구하는 함수 정의
def calculate_quotient_remainder(x, divisor):
quotient = x // divisor # 몫
remainder = x % divisor # 나머지
if x < 0: # 음수일 경우
return -quotient, -remainder
else: # 양수일 경우
return quotient, remainder
# divisor 설정
divisor = 7
# apply 메서드를 사용하여 몫과 나머지를 계산
df[['몫', '나머지']] = df['수치'].apply(lambda x: calculate_quotient_remainder(x, divisor)).apply(pd.Series)
# 결과 출력
print(df)
import pandas as pd
import numpy as np
# 값 설정
a = -140
b = 26
# 몫과 나머지 계산
quotient = np.floor_divide(a, b)
remainder = np.remainder(a, b)
# 결과 출력
print("몫:", quotient)
print("나머지:", remainder)