• 数据仓库与数据挖掘实验练习题


    练习题2

    1. 使用超级英雄列表来填充一个新的 Series 对象。
    2. 使用力量元组来填充一个新的 Series 对象。
    3. 创建一个 Series,将超级英雄作为索引标签,力量等级作为值。将这个 Series 赋值给 heroes 变量。
    4. 提取 heroes Series 的前两行。
    5. 提取 heroes Series 的最后四行。
    6. 确定 heroes Series 中唯一值的个数。
    7. 计算 heroes 中超级英雄的平均力量。
    8. 计算 heroes 中的最大和最小力量。
    9. 计算每个超级英雄的力量等级翻倍后的值。
    10. 将 heroes Series 转换为 Python 字典。

    1. #给出列表
    2. superheros = [
    3. 'Batman',
    4. 'Superman',
    5. 'Spider-man',
    6. 'Iron man',
    7. 'Captain America',
    8. 'Wonder Woman'
    9. ]
    10. strength_levels = (100, 120, 90, 95, 110, 120)
    11. #1.转为series对象(列表)
    12. pd.Series(superheros)
    13. #2.力量值转为series对象
    14. pd.Series(strength_levels)
    15. #3.创建series
    16. heros = pd.Series(
    17. data = strength_levels,
    18. index = superheros
    19. )
    20. heros
    21. #4.
    22. heros.head(2)
    23. #5.
    24. heros.tail(4)
    25. #6.
    26. heros.nunique()
    27. #7.
    28. heros.mean()
    29. #8.
    30. heros.max()
    31. heros.min()
    32. #9.
    33. heros * 2
    34. #10.
    35. dict(heros)

    练习题3

    战争期间,一周中哪一天发生的战斗最多

    1. import pandas as pd
    2. import datetime as dt
    3. #一周中每天转为星期形式
    4. def day_of_week(day):
    5. return day.strftime('%A')
    6. #战斗最多的一天(列出开始日期)
    7. days_of_war = pd.read_csv(filepath_or_buffer='revolutionary_war.csv',
    8. usecols=['Start Date'],
    9. parse_dates=['Start Date'],
    10. ).squeeze(1)
    11. print(type(days_of_war))
    12. print()
    13. days_of_war
    14. #dropna() 方法会删除 Series 中的任何包含 NaN(缺失值)的行
    15. #apply(day_of_week) 会对剩余的每个非空元素应用自定义函数 day_of_week。
    16. #这个操作将把日期转换为相应的星期几,将结果存储在名为 days 的新 Series 中。
    17. days = days_of_war.dropna().apply(day_of_week)
    18. days
    19. #统计不同星期出现的次数
    20. days.value_counts()

    练习题4

    1. 导入nfl.csv,将Birthday转为datetimes

    2. 用2种方法将DataFrame的index设置为name

    3. 统计每个队伍的球员数量

    4. 查找工资最高的5个球员

    5. 排序:先将team按字母顺序排序,再将salary按降序排序

    6. New York Jets roster队中年龄最大的球员是谁,他的生日是什么时候

    1. # 1. 导入nfl.csv,将Birthday转为datetimes
    2. nfl = pd.read_csv('nfl.csv', parse_dates=['Birthday'])
    3. nfl
    4. # 2. 用2种方法将DataFrame的index设置为name
    5. # 第一种方法
    6. nfl = nfl.set_index('Name')
    7. nfl
    8. # 第二种方法
    9. nfl_2 = pd.read_csv('nfl.csv', parse_dates=['Birthday'], index_col='Name')
    10. nfl
    11. # 3. 统计每个队伍的球员数量
    12. nfl['Team'].value_counts()
    13. # 4. 查找工资最高的5个球员
    14. nfl.sort_values(by=['Salary'], ascending=False).head(5)
    15. # 5. 排序
    16. # 先将team按字母顺序排序
    17. # 再将salary按降序排序
    18. nfl.sort_values(by=['Team', 'Salary'], ascending=[True, False])
    19. # 6. New York Jets roster队中年龄最大的球员是谁,他的生日是什么时候
    20. nfl = nfl.reset_index().set_index('Team')
    21. nfl
    22. nfl.loc['New York Jets']
    23. nfl.loc['New York Jets'].sort_values('Birthday').head(1)

    练习题5

    1. 优化数据集以限制内存使用并最大化效用。 这可能包括选择合适的数据类型、删除不必要的列等操作。
    2. 找到所有标题为 "Limitless" 的行。
    3. 找到所有导演为 "Robert Rodriguez" 且类型为 "Movie" 的行。
    4. 找到所有添加日期为 "2019-07-31" 或导演为 "Robert Altman" 的行。
    5. 找到所有导演为 "Orson Welles"、"Aditya Kripalani" 或 "Sam Raimi" 的行。
    6. 找到所有添加日期在 2019 年 5 月 1 日至 2019 年 6 月 1 日之间的行。
    7. 删除导演列中包含 NaN 值的所有行。
    8. 确定 Netflix 只在其目录中添加了一部电影的日期。
    1. # 优化数据集以限制内存使用并最大化效用
    2. netflix = pd.read_csv('netflix.csv', parse_dates=['date_added'])
    3. netflix.info()
    4. netflix.nunique()
    5.  
    6. netflix['type'] = netflix['type'].astype('category')
    7. netflix.info()
    8.  
    9. # 找到所有标题为 "Limitless" 的行。
    10. title = netflix['title'] == 'Limitless'
    11. netflix[title]
    12.  
    13.  
    14. # 找到所有导演为 "Robert Rodriguez" 且类型为 "Movie" 的行。
    15. director = (netflix['director'] == 'Robert Rodriguez')
    16. typeMovie = netflix['type'] == 'Movie'
    17. netflix[director & typeMovie]
    18. # 找到所有添加日期为 "2019-07-31" 或导演为 "Robert Altman" 的行。
    19. date = netflix['date_added'] == '2019-07-31'
    20. director = netflix['director'] == 'Robert Altman'
    21. netflix[date | director]
    22.  
    23. # 找到所有导演为 "Orson Welles"、"Aditya Kripalani" 或 "Sam Raimi" 的行
    24. directors = ['Orson Welles', 'Aditya Kripalani', 'Sam Raimi']
    25. target = netflix['director'].isin(directors)
    26. netflix[target]
    27.  
    28. # 找到所有添加日期在 2019 年 5 月 1 日至 2019 年 6 月 1 日之间的行。
    29. addMovie = netflix['date_added'].between('2019-5-1', '2019-6-1')
    30. netflix[addMovie]
    31.  
    32.  
    33. # 删除导演列中包含 NaN 值的所有行。
    34. netflix.dropna(subset = ['director'])
    35.  
    36.  
    37. # 确定 Netflix 只在其目录中添加了一部电影的日期。
    38. netflix.drop_duplicates(subset=['date_added'], keep=False)

    练习题6

    # customers.csv包括一个地址列。
    # 每个地址由一条街道、城市、州和邮政编码组成。
    # 分离这四个值;
    # 将它们分配到DataFrame中新的Street、City、State和Zip列;
    # 然后删除地址列。

    1. customers = pd.read_csv('customers.csv')
    2. customers
    3. customers['Address'].values
    4. split = customers['Address'].str.split(',' , expand = True)
    5. split
    6. customers[['Street', 'City', 'State', 'Zip']] = split
    7. customers = customers.drop(labels='Address', axis='columns')
    8. customers

    练习题8

    1 在 cars 数据集中,聚合汽车价格的总和。在行轴上按燃料类型分组结果。
    2 在 cars 数据集中,聚合汽车的数量。在索引轴上按制造商分组,在列轴上按变速箱类型分组。显示行和列的子总数。
    3 在 cars 数据集中,聚合汽车价格的平均值。在索引轴上按年份和燃料类型分组,在列轴上按变速箱类型分组。
    4 给定上一个挑战中的 DataFrame,将变速箱级别从列轴移动到行轴。
    5 将 min_wage 从宽格式转换为窄格式。换句话说,将数据从八个年份列(2010-17)移动到单个列中。

    1. car = pd.read_csv('used_cars.csv')
    2. car
    3. min_wage = pd.read_csv('minimum_wage.csv')
    4. min_wage.head()
    5. #1.
    6. car.pivot_table(
    7. values = 'Price',
    8. index = 'Fuel',
    9. aggfunc = 'sum'
    10. )
    11. #2.
    12. car.pivot_table(
    13. values = 'Price',
    14. index = 'Manufacturer',
    15. columns = 'Transmission',
    16. aggfunc = 'count',
    17. margins = True,
    18. margins_name = 'Total'
    19. )
    20. #3.
    21. car.pivot_table(
    22. values = 'Price',
    23. index = ['Year','Fuel'],
    24. columns = 'Transmission',
    25. aggfunc = 'mean'
    26. )
    27. #4.
    28. c1 = car.pivot_table(
    29. values = 'Price',
    30. index = ['Year','Fuel'],
    31. columns = 'Transmission',
    32. aggfunc = 'mean'
    33. )
    34. c1.stack()
    35. #5.
    36. year = ['2010','2011','2012','2013','2014','2015','2016','2017']
    37. min_wage.melt(id_vars = 'State',var_name = 'Year',value_name = 'wage')

  • 相关阅读:
    5.SpringBoot⾼级配置
    八环氧环己基乙基笼状聚倍半硅氧|八苯胺丙基poss
    【JAVAEE框架】Mybatis项目起步讲解
    SCA算法优化脉冲耦合神经网络的图像自动分割(Matlab代码实现)
    Java学习的知识笔记
    SpringBoot项目自定义注解实现RBAC权限校验
    [python]十九、进程、线程和协程
    关于mac下pycharm旧版本没删除的情况下新版本2023安装之后闪退
    Lucas在与位运算有关的组合数中的应用
    时间序列预测实战(十六)PyTorch实现GRU-FCN模型长期预测并可视化结果
  • 原文地址:https://blog.csdn.net/qq_64684039/article/details/139465814