๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Python/Pandas

[Pandas] cut(), qcut()์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ๊ฐ„ํ™”

by ISLA! 2023. 9. 7.

๐Ÿ cut( ) : ๋ฐ์ดํ„ฐ ์ˆ˜์— ๊ด€๊ณ„์—†์ด ๊ฐ™์€ ๊ธธ์ด๋กœ ๋‚˜๋ˆ”

 

cut()

  • pd.cut(๊ตฌ๊ฐ„ํ™”ํ•˜๊ณ  ์‹ถ์€ ์ปฌ๋Ÿผ, bins = ๊ตฌ๊ฐ„์ˆ˜, labels = ['๋ผ๋ฒจ๋ช…1', '๋ผ๋ฒจ๋ช…2'...])

 

[์˜ˆ์ œ1]
copied_data = df.copy()
copied_data['binning'] = pd.cut(copied_data['box_off_num'], bins = 3)
copied_data.head(3)

[์˜ˆ์ œ2]
copied_data = df.copy()
copied_data['binning'] = pd.cut(copied_data['box_off_num'], bins = 3, labels = ["A", "B", "C"])
copied_data.head(3)

์˜ˆ์ œ1 ๊ฒฐ๊ณผ
์˜ˆ์ œ2 ๊ฒฐ๊ณผ


๐Ÿ qcut( ) : ๊ฐ ๊ตฌ๊ฐ„์— ๋™์ผํ•œ ์ˆ˜์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๋„๋ก ๋‚˜๋ˆ”

qcut()

  • pd.qcut(๋‚˜๋ˆ„๊ณ  ์‹ถ์€ ์—ด, bins = ๋‚˜๋ˆ„๊ณ  ์‹ถ์€ ์ˆซ์ž, labels = [๋‚˜๋ˆˆ ๊ตฌ๊ฐ„๋ณ„๋กœ ๊ฐ’์„ ์ง€์ • ; ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž])

 

copied_data = df.copy()

copied_data['binning'] = pd.cut(copied_data['time'], bins = 4, labels = [1,2,3,4])

copied_data.head(3)

728x90