Python解题思路代码

  1. 程序的运行结果为:
    arr = np.array([[1, 2, 3, 4, 5],[4, 5, 6, 7, 8], [7, 8, 9, 10, 11]])
    print('一:索引结果为:',arr[1,2:5])
    print('二:索引结果为:',arr[0:,2:])
    print('三;索引结果为:',arr[:,4])
    表1:meal_detail.csv
  2. 程序的运行结果为:
    detail= pd.read_csv('meal_detail1.csv',encoding='gbk')print('一: ', detail.size)
    print('二:', detail.columns)
    print('三;', detail.shape)
  3. 程序的运行结果为:
    detail= pd.read_csv('meal_detail1.csv',encoding='gbk')
    dishes_name = detail.iloc[:,3]orderDish = detail.loc[:,['order_id','dishes_name']]
    print('使用iloc提取列为:', dishes_name)
    print('使用loc提取order_id和dishes_name列的size为:', orderDish)
  4. 程序的运行结果为:
    detail= pd.read_csv('meal_detail1.csv',encoding='gbk')detailGroup= detail[['order_id','counts','amounts']].groupby(by = 'order_id')
    print('订单详情表分组后每组的均值为:\n', detailGroup.mean().head())print('订单详情表分组后每组的大小为:','\n', detailGroup.size().head())(备注:detailGroup.mean()保留小数点后2位)

    img


一:索引结果为: [6 7 8]
二:索引结果为: [[ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
三;索引结果为: [ 5  8 11]
一:  28
二: Index(['order_id', 'dishes_name', 'counts', 'amounts'], dtype='object')
三; (7, 4)
使用iloc提取列为: 0    10
1    30
2    20
3    20
4     5
5    40
6    30
Name: amounts, dtype: int64
使用loc提取order_id和dishes_name列的size为:    order_id dishes_name
0       301        蒜蓉生蚝
1       301       蒙古烤鸡腿
2       413        大蒜苋菜
3       413       芝麻烤紫菜
4       413         蒜香包
5       417         白斩鸡
6       417        香烤牛排
订单详情表分组后每组的均值为:
             counts  amounts
order_id                   
301       3.000000     20.0
413       3.333333     15.0
417       1.000000     35.0
订单详情表分组后每组的大小为: 
 order_id
301    2
413    3
417    2
dtype: int64