1. 读取数据并输出包含缺失值的行

import pandas as pd

df = pd.read_csv('drinks.csv') print(df[df.isnull().T.any()])

  1. 将NA替换为字符串NA

df = pd.read_csv('drinks.csv', na_values='') df['Continent'].fillna('NA', inplace=True)

  1. 输出各个大洲的平均啤酒、烈酒和红酒的消费量

beer_mean = df.groupby('Continent')['beer_servings'].mean() spirit_mean = df.groupby('Continent')['spirit_servings'].mean() wine_mean = df.groupby('Continent')['wine_servings'].mean()

print('平均啤酒消费量:\n', beer_mean) print('平均烈酒消费量:\n', spirit_mean) print('平均红酒消费量:\n', wine_mean)

  1. 输出啤酒、烈酒和红酒消费量最高的国家

beer_top = df[['Country', 'beer_servings']].sort_values(by='beer_servings', ascending=False).iloc[0] spirit_top = df[['Country', 'spirit_servings']].sort_values(by='spirit_servings', ascending=False).iloc[0] wine_top = df[['Country', 'wine_servings']].sort_values(by='wine_servings', ascending=False).iloc[0]

print('啤酒消费量最高的国家:', beer_top[0], beer_top[1]) print('烈酒消费量最高的国家:', spirit_top[0], spirit_top[1]) print('红酒消费量最高的国家:', wine_top[0], wine_top[1]

一实验目的1掌握Series和DataFrame的创建;2熟悉pandas数据清洗和数据分析的常用操作;3掌握使用matplotlib库画图的基本方法。二实验平台1操作系统:Windows系统;2Python版本:387三实验步骤 酒类消费数据给定一个某段时间内各个国家的酒类消费数据表drinkscsv其中包含6个字段表8-1给出了该表中的字段信息。表6-1 酒类消费数据表的字段信息Country

原文地址: http://www.cveoy.top/t/topic/hunU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录