900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Python for Data Analysis_2nd_Task 5 之 Pandas 进阶

Python for Data Analysis_2nd_Task 5 之 Pandas 进阶

时间:2022-12-16 16:47:09

相关推荐

Python for Data Analysis_2nd_Task 5 之 Pandas 进阶

Python for Data Analysis_2nd_Task 5 之 Pandas 进阶

十道经典练习,使用Pandas,一起玩转数据分析

开始了解你的数据:探索 Chipotle 快餐数据数据过滤排序:探索 欧洲杯数据数据分组:探索酒类消费数据Apply函数:探索 1960- 美国犯罪数据合并:探索虚拟姓名数据统计:探索风速数据可视化:探索泰坦尼克灾难数据创建数据框:探索 Pokemon 数据时间序列:探索 Apple 公司股价数据删除数据:探索 Iris 纸鸢花数据

开始了解你的数据:探索 Chipotle 快餐数据

探索 Chipotle 快餐数据

查看对应的数据集文件路径

ls ../input/pandas_exercise/exercise_data/Apple_stock.csv drinks.csvsecond_cars_info.csvwechart.csvcars.csv Euro_stats.csv train.csv wind.datachipotle.tsviris.csv US_Crime_Rates_1960_.csv

Step 1 导入必要的库

import pandas as np

Step 2 导入数据集

path1 = "../input/pandas_exercise/exercise_data/chipotle.tsv"

Step 3 将数据集存入 chipo 的 DataFrame 类型

chipo = pd.read_csv(path1, sep = '\t')

Step 4 查看前 10 行内容

chipo.head(10)order_id quantity item_name choice_description item_price0 1 1 Chips and Fresh Tomato Salsa NaN $2.391 1 1 Izze [Clementine] $3.392 1 1 Nantucket Nectar [Apple] $3.393 1 1 Chips and Tomatillo-Green Chili Salsa NaN $2.394 2 2 Chicken Bowl [Tomatillo-Red Chili Salsa (Hot), [Black Beans... $16.985 3 1 Chicken Bowl [Fresh Tomato Salsa (Mild), [Rice, Cheese, Sou... $10.986 3 1 Side of Chips NaN $1.697 4 1 Steak Burrito [Tomatillo Red Chili Salsa, [Fajita Vegetables... $11.758 4 1 Steak Soft Tacos [Tomatillo Green Chili Salsa, [Pinto Beans, Ch... $9.259 5 1 Steak Burrito [Fresh Tomato Salsa, [Rice, Black Beans, Pinto... $9.25

Step 5 产看数据集中的列数

# columnschipo.shape[1]5# rowschipo.shape[0]4622

Step 6 打印全部列名

chipo.columnsIndex(['order_id', 'quantity', 'item_name', 'choice_description','item_price'],dtype='object')chipo.info # [4622 rows x 5 columns]<bound method DataFrame.info of order_id quantityitem_name \0 1 1 Chips and Fresh Tomato Salsa 1 1 1Izze 2 1 1 Nantucket Nectar 3 1 1 Chips and Tomatillo-Green Chili Salsa 4 2 2 Chicken Bowl 5 3 1 Chicken Bowl 6 3 1Side of Chips 7 4 1Steak Burrito 8 4 1 Steak Soft Tacos 9 5 1Steak Burrito 10 5 1Chips and Guacamole 11 6 1 Chicken Crispy Tacos 12 6 1 Chicken Soft Tacos 13 7 1 Chicken Bowl 14 7 1Chips and Guacamole 15 8 1 Chips and Tomatillo-Green Chili Salsa 16 8 1 Chicken Burrito 17 9 1 Chicken Burrito 18 9 2 Canned Soda 1910 1 Chicken Bowl 1Chips and Guacamole 2111 1 Barbacoa Burrito 2211 1 Nantucket Nectar 2312 1 Chicken Burrito 2412 1Izze 2513 1 Chips and Fresh Tomato Salsa 2613 1 Chicken Bowl 2714 1 Carnitas Burrito 2814 1 Canned Soda 2915 1 Chicken Burrito ... ... ...... 45921825 1 Barbacoa Burrito 45931825 1Carnitas Bowl 45941825 1Barbacoa Bowl 45951826 1 Chicken Bowl 45961826 1Chips and Guacamole 45971826 1 Canned Soft Drink 45981826 1Bottled Water 45991827 1 Chicken Bowl 46001827 1Chips and Guacamole 46011827 1 Canned Soft Drink 46021827 1 Barbacoa Burrito 46031827 1 Barbacoa Burrito 46041828 1 Chicken Bowl 46051828 1Chips and Guacamole 46061828 1 Canned Soft Drink 46071829 1Steak Burrito 46081829 1Veggie Burrito 46091829 1 Canned Soft Drink 46101830 1Steak Burrito 46111830 1Veggie Burrito 46121831 1Carnitas Bowl 46131831 1 Chips 46141831 1Bottled Water 46151832 1 Chicken Soft Tacos 46161832 1Chips and Guacamole 46171833 1Steak Burrito 46181833 1Steak Burrito 46191834 1 Chicken Salad Bowl 4634 1 Chicken Salad Bowl 46211834 1 Chicken Salad Bowl choice_description item_price 0 NaN$2.39 1 [Clementine]$3.39 2 [Apple]$3.39 3 NaN$2.39 4[Tomatillo-Red Chili Salsa (Hot), [Black Beans... $16.98 5[Fresh Tomato Salsa (Mild), [Rice, Cheese, Sou... $10.98 6 NaN$1.69 7[Tomatillo Red Chili Salsa, [Fajita Vegetables... $11.75 8[Tomatillo Green Chili Salsa, [Pinto Beans, Ch...$9.25 9[Fresh Tomato Salsa, [Rice, Black Beans, Pinto...$9.25 10NaN$4.45 11 [Roasted Chili Corn Salsa, [Fajita Vegetables,...$8.75 12 [Roasted Chili Corn Salsa, [Rice, Black Beans,...$8.75 13 [Fresh Tomato Salsa, [Fajita Vegetables, Rice,... $11.25 14NaN$4.45 15NaN$2.39 16 [Tomatillo-Green Chili Salsa (Medium), [Pinto ...$8.49 17 [Fresh Tomato Salsa (Mild), [Black Beans, Rice...$8.49 18[Sprite]$2.18 19 [Tomatillo Red Chili Salsa, [Fajita Vegetables...$8.75 20NaN$4.45 21 [[Fresh Tomato Salsa (Mild), Tomatillo-Green C...$8.99 22 [Pomegranate Cherry]$3.39 23 [[Tomatillo-Green Chili Salsa (Medium), Tomati... $10.98 24 [Grapefruit]$3.39 25NaN$2.39 26 [Roasted Chili Corn Salsa (Medium), [Pinto Bea...$8.49 27 [[Tomatillo-Green Chili Salsa (Medium), Roaste...$8.99 28 [Dr. Pepper]$1.09 29 [Tomatillo-Green Chili Salsa (Medium), [Pinto ...$8.49 ... ... ... 4592 [Tomatillo Red Chili Salsa, [Rice, Fajita Vege... $11.75 4593 [Roasted Chili Corn Salsa, [Rice, Sour Cream, ... $11.75 4594 [Roasted Chili Corn Salsa, [Pinto Beans, Sour ... $11.75 4595 [Tomatillo Green Chili Salsa, [Rice, Black Bea...$8.75 4596 NaN$4.45 4597 [Nestea]$1.25 4598 NaN$1.50 4599[Roasted Chili Corn Salsa, [Cheese, Lettuce]]$8.75 4600 NaN$4.45 4601[Diet Coke]$1.25 4602 [Tomatillo Green Chili Salsa]$9.25 4603 [Tomatillo Green Chili Salsa]$9.25 4604 [Fresh Tomato Salsa, [Rice, Black Beans, Chees...$8.75 4605 NaN$4.45 4606[Coke]$1.25 4607 [Tomatillo Green Chili Salsa, [Rice, Cheese, S... $11.75 4608 [Tomatillo Red Chili Salsa, [Fajita Vegetables... $11.25 4609 [Sprite]$1.25 4610 [Fresh Tomato Salsa, [Rice, Sour Cream, Cheese... $11.75 4611 [Tomatillo Green Chili Salsa, [Rice, Fajita Ve... $11.25 4612 [Fresh Tomato Salsa, [Fajita Vegetables, Rice,...$9.25 4613 NaN$2.15 4614 NaN$1.50 4615 [Fresh Tomato Salsa, [Rice, Cheese, Sour Cream]]$8.75 4616 NaN$4.45 4617 [Fresh Tomato Salsa, [Rice, Black Beans, Sour ... $11.75 4618 [Fresh Tomato Salsa, [Rice, Sour Cream, Cheese... $11.75 4619 [Fresh Tomato Salsa, [Fajita Vegetables, Pinto... $11.25 4620 [Fresh Tomato Salsa, [Fajita Vegetables, Lettu...$8.75 4621 [Fresh Tomato Salsa, [Fajita Vegetables, Pinto...$8.75 [4622 rows x 5 columns]>chipo.describe()order_id quantitycount 4622.000000 4622.000000mean 927.254868 1.075725std 528.890796 0.410186min 1.000000 1.00000025% 477.250000 1.00000050% 926.000000 1.00000075% 1393.000000 1.000000max 1834.000000 15.000000

Step 7 查看数据集的索引

chipo,indexRangeIndex(start=0, stop=4622, step=1)

Step 8 查看下单最多的商品 (item)

c = chipo[['item_name', 'quantity']].groupby(['item_name'], as_index=False).agg({'quantity':sum})c.sort_values(['quantity'], ascending=False, inplace=True)c.head()item_name quantity17 Chicken Bowl 76118 Chicken Burrito 59125 Chips and Guacamole 50639 Steak Burrito 38610 Canned Soft Drink 351

Step 9 在指定列中,产看商品下单的种类

chipo['item_name'].nuique()50

Step 10 在指定列choice_description中,查看下单最多的商品名

chipo['choice_description'].value_counts().head()[Diet Coke] 134[Coke] 123[Sprite] 77[Fresh Tomato Salsa, [Rice, Black Beans, Cheese, Sour Cream, Lettuce]]42[Fresh Tomato Salsa, [Rice, Black Beans, Cheese, Sour Cream, Guacamole, Lettuce]]40Name: choice_description, dtype: int64

Step 11 一共有多少商品被下单

total_items_orders = chipo['quantity'].sum()total_items_orders4972

Step 12 将 item_price 转换为 float 类型

dollarizer = lamnda x : float(x[1:-1])chipo['item_price'] = chipo['item_price'].apply(dollarizer)02.3913.3923.3932.394 16.98Name: item_price, dtype: float64

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。