<jupyter_start><jupyter_text>Ex2 - Getting and Knowing your Data This time we are going to pull data directly from the internet.Special thanks to: https://github.com/justmarkham for sharing the dataset and materials. Step 1. Import the necessary libraries<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Step 2. Import the dataset from this [address](https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv).<jupyter_code>url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv'<jupyter_output><empty_output><jupyter_text>- Bởi vì đây là `.tsv` (\t), nên khi sử dụng hàm `read_csv()`, ta cần phải xác định thêm argument `sep='\t'`<jupyter_code>df = pd.read_csv(url, sep='\t')
df.head()<jupyter_output><empty_output><jupyter_text>Step 3. Assign it to a variable called chipo.<jupyter_code>chipo = df<jupyter_output><empty_output><jupyter_text>Step 4. See the first 10 entries<jupyter_code>chipo.head(10)<jupyter_output><empty_output><jupyter_text>Step 5. What is the number of observations in the dataset?<jupyter_code># Solution 1
chipo.shape

# Solution 2
chipo.info()
# Hoặc cũng có thể chipo.count()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 4622 entries, 0 to 4621
Data columns (total 5 columns):
 #   Column              Non-Null Count  Dtype 
---  ------              --------------  ----- 
 0   order_id            4622 non-null   int64 
 1   quantity            4622 non-null   int64 
 2   item_name           4622 non-null   object
 3   choice_description  3376 non-null   object
 4   item_price          4622 non-null   object
dtypes: int64(2), object(3)
memory usage: 180.7+ KB<jupyter_text>Step 6. What is the number of columns in the dataset?<jupyter_code>len(chipo.columns)<jupyter_output><empty_output><jupyter_text>Step 7. Print the name of all the columns.<jupyter_code>chipo.columns<jupyter_output><empty_output><jupyter_text>Step 8. How is the dataset indexed?<jupyter_code>chipo.index<jupyter_output><empty_output><jupyter_text>Step 9. Which was the most-ordered item?  - Ở đây ta không thể dùng `chipo['item_name'].value_counts()` vì hàm này chỉ đếm số lần xuất hiện của các giá trị trong cột `item_name`.- Cái chúng ta cần ở đây là `số lượng (quantity)`, mỗi order có thể order 1 cho đến nhiều item, nên cần phải tính tổng các `quantity` đã order cho từng giá trị trong cột `item_name`<jupyter_code>c = chipo.groupby('item_name')
c = c.sum()
c = c.sort_values('quantity', ascending=False)
c.head(1)<jupyter_output><empty_output><jupyter_text>Step 10. For the most-ordered item, how many items were ordered?<jupyter_code>c = chipo.groupby('item_name')
c = c.sum()
c = c.sort_values('quantity', ascending=False)
c.head(1)<jupyter_output><empty_output><jupyter_text>Step 11. What was the most ordered item in the choice_description column?<jupyter_code>c = chipo.groupby('choice_description').sum()
c = c.sort_values('quantity', ascending=False)
c.head(1)<jupyter_output><empty_output><jupyter_text>Step 12. How many items were orderd in total?<jupyter_code>chipo['quantity'].sum()<jupyter_output><empty_output><jupyter_text>Step 13. Turn the item price into a float  Step 13.a. Check the item price type<jupyter_code>chipo.item_price.dtype<jupyter_output><empty_output><jupyter_text>Step 13.b. Create a lambda function and change the type of item price<jupyter_code># Solution 1
#chipo.item_price = chipo.item_price.apply(lambda x: x.replace('$', '')).astype('float')
# Solution 2
dollar = lambda x: float(x[1:-1])
chipo.item_price = chipo.item_price.apply(dollar)<jupyter_output><empty_output><jupyter_text>Step 13.c. Check the item price type<jupyter_code>chipo.item_price.dtype<jupyter_output><empty_output><jupyter_text>Step 14. How much was the revenue for the period in the dataset?<jupyter_code>revenue = (chipo['quantity'] * chipo['item_price']).sum()
print("Revenue: $", revenue, sep="")<jupyter_output>Revenue: $39237.02<jupyter_text>Step 15. How many orders were made in the period?<jupyter_code>order = chipo['order_id'].value_counts().count()
chipo.sort_values('order_id', ascending=False)<jupyter_output><empty_output><jupyter_text>Step 16. What is the average revenue amount per order?<jupyter_code># Solution 1
avg_revenue = (chipo['quantity'] * chipo['item_price']).sum() / order
avg_revenue
# Solution 2
chipo['revenue'] = chipo['quantity'] * chipo['item_price']
chipo.groupby(by=["order_id"]).sum().mean()['revenue']<jupyter_output><empty_output><jupyter_text>Step 17. How many different items are sold?<jupyter_code>chipo['item_name'].value_counts().count()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12627027027027027, 'fraction_numerical': 0.022486486486486486, 'mean_word_length': 5.7631578947368425, 'pattern_counts': {'":': 0, '<': 80, '<?xml version=': 0, '>': 80, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '159356', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1718, 'n_words': 395, 'path': '01_Getting_&_Knowing_Your_Data/Chipotle/Exercises.ipynb', 'repo_name': 'vuongtruongson99/pandas_exercises', 'size': '4625'}