[Python] ์นดํ ๊ณ ๋ฆฌ ๋ถ๋ฅํ๊ธฐโก๏ธ
# ํ๋ก์ ํธ ์ฃผ์ : ์๋ง์กด ๊ณ ๊ฐ ๊ตฌ๋งค ์ํ ๋ฐ์ดํฐ ๋ถ์
# ์ปฌ๋ผ ์ค๋ช
โ๏ธDiscount Amount : ํ ์ธ๊ธ์ก
โ๏ธList Amount : ์ ๊ฐ / ํ ์ธ์ ๊ธ์ก
โ๏ธSales Amount : ์ค์ ํ๋งค๊ธ์ก
-> Sales Price * Sales Quantity
โ๏ธSales Amount Based on List Price : ํ ์ธ์ ์ฉ ์๋ ํ๋งค๊ธ์ก ์ ์ฒด
โ๏ธSales Cost Amount : ์ํ์ ํ๋งคํ๋๋ฐ ๋ค์ด๊ฐ ๋น์ฉ
โ๏ธSales Margin Amount : ํ๋งค ๋ง์ง ๊ธ์ก
-> Sales Amount - Sales Cost Amount
โ๏ธSales Price : ์ค์ ํ๋งค ๊ฐ๊ฒฉ
โ๏ธSales Quantity : ์ํ ์๋
< ์นดํ ๊ณ ๋ฆฌ ๋ถ๋ฅํ๊ธฐ >
1. ๋ฐ์ดํฐ ๋ก๋ํ๊ธฐ
์ฌ๋ฌ ํจํค์ง ๊ฐ์ ธ์ค๊ธฐ
import pandas as pd
import sklearn
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import os
import re
csv ํ์ผ ์ฝ์ด์ค๊ธฐ
df = pd.read_csv('base_df.csv')
df = df.dropna()
โก๏ธnan ํ ์ปฌ๋ผ ๋ฒ๋ฆฌ๊ธฐ
df = df.dropna()
โก๏ธํน์ nan ํ ์ปฌ๋ผ ๋ฒ๋ฆฌ๊ธฐ
df = df.dropna(subset = ['์ปฌ๋ผ1', '์ปฌ๋ผ2'])
โก๏ธ์ด ์ปฌ๋ผ ๋ฒ๋ฆฌ๊ธฐ
df = df.drop('์ปฌ๋ผ1', axis=1)
df ํ์ธํ๊ธฐ
df
Item ์ปฌ๋ผ ํ์ธํ๊ธฐ
df['Item']
2. ๋ง์ง๋ง 2๋จ์ด๋ก Product ์ปฌ๋ผ ์์ฑํ๊ธฐ
df['Product'] = df['Item'].apply(lambda x : ' '.join(x.split()[-2:]) if len(x.split()) >= 2 else x)
โก๏ธ์ค์ํ ๋ฌธ๋ฒ
์ปฌ๋ผ.apply(lambda x : ์)
groupby(์ปฌ๋ผ).apply(lambda x : ์)
โก๏ธ๋ฌธ์์ด ์์ 2์๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ
x.split()[:2]
๋ง์ฝ 2์๋ฆฌ์ธ ๊ฒฝ์ฐ๋ ์์ ' '.join()์ ํจ์ผ๋ก์จ ๋ฌธ์์ด ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ๋ฌถ์ด์ค์ผ ํ๋ค
โก๏ธ๋ฌธ์์ด ๋ค์ 2์๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ
x.split()[-2:]
๋ง์ฝ 2์๋ฆฌ์ธ ๊ฒฝ์ฐ๋ ์์ ' '.join()์ ํจ์ผ๋ก์จ ๋ฌธ์์ด ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ๋ฌถ์ด์ค์ผ ํ๋ค
df ํ์ธํ๊ธฐ
df
df['Product']์ ์ ๋ํฌ ๊ฐ ํ์ธํ๊ธฐ
df['Product'].unique()
๋ง์ฝ ์ฌ๊ธฐ์ text editor๋ฅผ ํด๋ฆญํ๋ค๋ฉด ์ ์ฒด ํ์ผ์ ๋ณผ ์ ์๋ค!
โก๏ธ์ซ์์ธ๊ธฐ ํจ์ ์ข ๋ฅ
1) count
์ค๋ณต์ ๊ฑฐ๋ฅผ ํ์ง ์๋๋ค
2) nunique
์ค๋ณต์ ๊ฑฐ ํ ๊ฐ์์ธ๊ธฐ
3) unique
๊ฐ์์ข ๋ฅ
4) value_counts
๊ฐ์ ์ข ๋ฅ๋ณ ๊ฐ์์ธ๊ธฐ
3. ์นดํ ๊ณ ๋ฆฌ ์ ์ ํ ๋ถ๋ฅํด๋ณด๊ธฐ
df_product = df[['Product']]
df_product
df['Product'].unique() ๋ฅผ ์ํํ ํ text editor๋ฅผ ์ด์ด์ ์งํผํฐ์๊ฒ ๊ฐ ์นดํ ๊ณ ๋ฆฌ๋ณ๋ก ๋ถ๋ฅํด๋ฌ๋ผ๊ณ ํด ๋ณด์๋ค
์นดํ ๊ณ ๋ฆฌ๋ณ๋ก ๋์ ๋๋ฆฌ ์์ฑํ๊ธฐ
# ์นดํ
๊ณ ๋ฆฌ ์ ์
categories = {
'Food': ['Butter', 'Rice', 'Soup', 'Pasta', 'Pizza', 'Sandwich', 'Cake', 'Cookie', 'Brownie', 'Candy', 'Chocolate', 'Noodle'],
'Vegetables': ['Vegetable', 'Potato', 'Onion', 'Carrot', 'Tomato', 'Lettuce', 'Broccoli', 'Asparagus', 'Mushroom', 'Pepper', 'Garlic'],
'Fruits': ['Apple', 'Orange', 'Banana', 'Lemon', 'Lime', 'Grape', 'Peach', 'Plum', 'Cherry', 'Berry', 'Melon'],
'Dairy': ['Milk', 'Cheese', 'Yogurt', 'Butter', 'Cream'],
'Beverages': ['Juice', 'Soda', 'Cola', 'Wine', 'Beer', 'Drink'],
'Snacks': ['Chips', 'Pretzels', 'Popcorn', 'Crackers', 'Jerky', 'Nuts', 'Mints', 'Waffles'],
'Bread': ['Bread', 'Bagel', 'Muffin', 'Donut', 'Roll'],
'Canned Food': ['Canned', 'Tuna', 'Sardines', 'Tomatos', 'Peaches', 'Beans', 'Corn', 'Soup'],
'Other': []
}
๋์ ๋๋ฆฌ items()์ผ๋ก ํค์ ๊ฐ์ ์ ํ์ธํ๊ธฐ
categories.items()
dict_items([('Food', ['Butter', 'Rice', 'Soup', 'Pasta', 'Pizza', 'Sandwich', 'Cake', 'Cookie', 'Brownie', 'Candy', 'Chocolate', 'Noodle']),
('Vegetables', ['Vegetable', 'Potato', 'Onion', 'Carrot', 'Tomato', 'Lettuce', 'Broccoli', 'Asparagus', 'Mushroom', 'Pepper', 'Garlic']),
('Fruits', ['Apple', 'Orange', 'Banana', 'Lemon', 'Lime', 'Grape', 'Peach', 'Plum', 'Cherry', 'Berry', 'Melon']),
('Dairy', ['Milk', 'Cheese', 'Yogurt', 'Butter', 'Cream']),
('Beverages', ['Juice', 'Soda', 'Cola', 'Wine', 'Beer', 'Drink']),
('Snacks', ['Chips', 'Pretzels', 'Popcorn', 'Crackers', 'Jerky', 'Nuts', 'Mints', 'Waffles']),
('Bread', ['Bread', 'Bagel', 'Muffin', 'Donut', 'Roll']),
('Canned Food', ['Canned', 'Tuna', 'Sardines', 'Tomatos', 'Peaches', 'Beans', 'Corn', 'Soup']),
('Other', [])])
โก๏ธ ์นดํ ๊ณ ๋ฆฌ ๋งคํ ํจ์
# ์นดํ
๊ณ ๋ฆฌ ๋งคํ ํจ์
def categorize_item(item):
for category, keywords in categories.items():
if any(re.search(r'\b' + keyword + r'\b', item, re.IGNORECASE) for keyword in keywords):
return category
return 'Other'
re.search(keyword, item, re.IGNORECASE)๋ item์์ ํค์๋๋ฅผ ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ๊ฒ์ํฉ๋๋ค.
ํค์๋๊ฐ item์ ํฌํจ๋์ด ์์ผ๋ฉด ํด๋น ์นดํ ๊ณ ๋ฆฌ๋ฅผ ๋ฐํํฉ๋๋ค.
๋ชจ๋ ์นดํ ๊ณ ๋ฆฌ์ ํด๋นํ์ง ์์ผ๋ฉด 'Other'๋ฅผ ๋ฐํํฉ๋๋ค.
# ๋จ์ด ๊ฒฝ๊ณ \b์ ์ญํ
\b๋ ๋ฌธ์์ ๊ณต๋ฐฑ ๋๋ ๋น ๋ฌธ์(์: ๊ตฌ๋์ ) ์ฌ์ด์ ์๋ ์์น๋ฅผ ๋งค์นญํฉ๋๋ค.
์๋ฅผ ๋ค์ด, \bword\b๋ 'word'๋ผ๋ ๋จ์ด๋ฅผ ์ ํํ ๋งค์นญํ๋ฉฐ, 'sword', 'wording' ๋ฑ๊ณผ ๊ฐ์ ๋ฌธ์์ด๊ณผ๋ ๋งค์นญํ์ง ์์ต๋๋ค.
๋ง์ฝ \b๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด word๋ผ๋ ๋จ์ด๋ฅผ ๋งค์นญํ ๋ sword์์๋ ๋งค์นญ๋๋ ์ํฉ์ด ๋์ฌ ์ ์์ต๋๋ค
df['Item']์ ์นดํ ๊ณ ๋ฆฌ ๋งคํ ํจ์ ์ ์ฉํ ์๋ก์ด ์ปฌ๋ผ ์นดํ ๊ณ ๋ฆฌ ์์ฑํ๊ธฐ
df['Category'] = df['Item'].apply(categorize_item)
์นดํ ๊ณ ๋ฆฌ ์์ฑ ํ์ธํ๊ธฐ
df['Category']
df
