1. ๋ฐ์ดํฐ ํ๋ ์ ๋๋ค์ถ์ถ
#https://rfriend.tistory.com/602
#df์์ ๋๋ค์ผ๋ก 1000๊ฐ๋ฅผ ๋ฝ๊ณ ์ถ์ ๋
df.sample(n=1000,replace=False) #๋น๋ณต์์ถ์ถ
df.sample(n=1000,replace=False) #๋ณต์์ถ์ถ
2. ํน์ ๊ธฐ๊ฐ ์ฌ์ด์ ๋ฐ์ดํฐ๋ง ์ถ์ถ
#https://happy-obok.tistory.com/m/5
from datetime import datetime
Data['timestamp']= pd.to_datetime(Data["timestamp"])
Data = Data[(Data.timestamp >= datetime(start_year,start_month , start_day)) & (Data.timestamp <= datetime(finish_year, finish_month, finish_day))]
3. ๋ฐ์ดํฐ ์ธ๋ฑ์ค ์ฐจ๊ณก์ฐจ๊ณก
Data.reset_index(drop=True, inplace=True)
4.reset_index ์ปฌ๋ผ์ด๋ฆ๊ณผ ํจ๊ป
Data.reset_index().rename(columns={"index": "index"})
5. ํน์ ์ปฌ๋ผ ์ ๊ฑฐ
#age ์ปฌ๋ผ์ ์ญ์ ํ ์ํ ์ถ๋ ฅ
df.drop('age', axis=1)
#age ์ปฌ๋ผ ์ญ์ ๋ด์ฉ์ ๋ฐ๋ก df์ ์ ์ฉ -> inplace=True
df.drop('age', axis=1, inplace=True)
#์ฌ๋ฌ๊ฐ์ ์ปฌ๋ผ ํ๊บผ๋ฒ์ ์ญ์
df.drop(['age','height'], axis=1, inplace=True)
6. ๊ฒฐ์ธก์น๊ฐฏ์
df.isnull().sum()
df['A'].isnull().sum() #A ์ปฌ๋ผ์์ ๊ฒฐ์ธก์น ๊ฐฏ์
df.notnull().sum()
7. ํน์ ์ปฌ๋ผ ๊ธฐ์ค์ผ๋ก ์ค๋ณต์ ๊ฑฐ
#์ด์ ๊ธฐ์ค์ผ๋ก ์ค๋ณต์ ๊ฑฐ
df.drop_duplicates(['asin'])
8. ํน์ ์ปฌ๋ผ ๊ธฐ์ค ํน์ ๋ฌธ์ ์ง์ฐ๊ธฐ
products_copy['price'] = products_copy['price'].str.replace('$','')
9. ๊ณต๋ฐฑ์ ๊ฑฐ
#์์ชฝ๊ณต๋ฐฑ ๋ชจ๋ ์ ๊ฑฐ
products["description"].str.strip()
10. ๊ฐํ๋ฌธ์ ์ ๊ฑฐ
#feature ์ค๋ฐ๊ฟ์ ๊ฑฐ
products_copy["feature"]= products_copy["feature"].replace(r'\\n','', regex=True)
11. ๋ฐ์ดํฐ๋ฅผ ๊ธธ์ด๊ธฐ์ค์ผ๋ก ํํฐ๋ง
#title์ ๊ธธ์ด๊ฐ 300 ๋ฏธ๋ง์ธ ๊ฒ๋ง ์ถ๋ ฅ
products_copy[products_copy.title.apply(lambda x: len(str(x))<300)]
12. ํน์ ๊ฐ์ ํฌํจํ ๋ฐ์ดํฐ๋ง ์ถ์ถ
#asin์ด asin_list์ ํฌํจ๋์ด์์ ๊ฒฝ์ฐ๋ง
products_reveiw_copy[products_reveiw_copy.asin.apply(lambda x: x in asin_list)]
13. ํน์ ์ปฌ๋ผ ๊ธฐ์ค์ผ๋ก merge
review_review_count = pd.merge(review_count_per_asin_df,products_reviewText_df, how='inner', on='asin')
14. ์๋ก์ด ์ปฌ๋ผ ์์ฑ๊ณผ ๋์์ list๋ก ๋ ๋ฐ์ดํฐ ์ถ๊ฐ
#4๋ฒ์งธ ์ปฌ๋ผ์ keywords๋ผ๋ ์ด๋ฆ์ผ๋ก ์์ฑ, result๋ผ๋ ๋ฆฌ์คํธ๋ฅผ ์๋ก์ด ๋ฐ์ดํฐ๋ก ๋ฃ๊ธฐ
merged_reivew_q_copy.insert(4,"keywords",result,True)
15. ๋ฐ์ดํฐํ๋ ์ ์ปฌ๋ผ์ ๋ฌธ์์ด์ด ์๋๋ผ ๋ฆฌ์คํธ๋จ์๋ก ์ฝ๊ธฐ
products.also_buy=products.also_buy.str[1:-1].str.split(',').tolist()
https://stackoverflow.com/questions/45758646/pandas-convert-string-into-list-of-strings
16. ํ๋ฐฉํฅ์ผ๋ก ์ด์ด๋ถ์ด๊ธฐ
pd.concat([df1, df2])
#or
df1.append(df2)
17. ํน์ ์ปฌ๋ผ๊ธฐ์ค ์ ๋ ฌ
#์ค๋ฆ์ฐจ์
data.sort_values("reviewCount")
#๋ด๋ฆผ์ฐจ์
data.sort_values("reviewCount",ascending=False)
18. string ์ปฌ๋ผ์ ํ๊ท ๊ธธ์ด
df["mergedReview"].str.len().mean()
19. ์๋ก์ด ์ปฌ๋ผ์ ๋ฆฌ์คํธ๋ก ๋ ๋ฐ์ดํฐ ์ถ๊ฐ
mergedReview_product.loc[:,'keywords'] = result
20. kw=[(a,1),(b,2)] → ๋ฆฌ์คํธ ์์ ์๋ ํํ์์ ์ฒซ๋ฒ์งธ ์์๋ง ๊ฐ์ ธ์ค๊ธฐ
[x[0] for x in kw]
21. ํน์ ์ด ๋ฌธ์์ด ๊ธธ์ด ์ค์ด๊ธฐ
#description ์ด์ ๊ธธ์ด๋ฅผ 50์ผ๋ก ์๋ฅด๊ธฐ
description["description"].astype(str).apply(lambda x: x[:50])
22. ๋ ์ง ์ปฌ๋ผ์์ ๋ ๋๋ง ์ถ๋ฆฌ๊ธฐ
data["year"] = data["year"].apply(lambda x : x[-5:-1]
23. group์ผ๋ก ๋ฌถ์ด์ ํน์ ์ปฌ๋ผ ํต๊ณ
df_review.groupby("asin")["review_length"].mean().values
list(df_review.groupby("asin")["review_length"].mean().values)
24. ํน์ ์ปฌ๋ผ ๊ธฐ์ค์ผ๋ก ์๋ก์ด ์ปฌ๋ผ์ ๊ฐ ์ถ๊ฐ
price_all['nlp'] = np.where(price_all.description.isnull(), 1,0)
description ์ปฌ๋ผ์ด nan์ผ ๋ 1์ ๋ถ์ด๊ณ ์๋๋ 0
'Programming > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Python ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ๋ง๋ค๊ธฐ (0) | 2021.06.22 |
---|---|
ValueError: Iterable over raw text documents expected, string object received. (0) | 2021.06.19 |
์์๋๋ฉด ์ข์ python ํจ์ - lambda, assert, map, filter (0) | 2021.03.25 |
python set ์์ ์ถ๊ฐ ๋ฐ ์ญ์ (0) | 2021.03.25 |
Python skew(), kurt() (0) | 2020.10.21 |