pandas数据清洗初级实践 一实验目的1掌握Series和DataFrame的创建;2熟悉pandas数据清洗和数据分析的常用操作;3掌握使用matplotlib库画图的基本方法。二实验平台1操作系统:Windows系统;2Python版本:387三实验步骤2 酒类消费数据给定一个某段时间内各个国家的酒类消费数据表drinkscsv其中包含6个字段表8-1给出了该表中的字段信息。表6-1 酒类消
- 读取数据并输出包含缺失值的行
首先,我们需要用pandas将酒类消费数据表中的数据读取为DataFrame,可以使用read_csv函数。
import pandas as pd df = pd.read_csv('drinks.csv') print(df[df.isnull().T.any()])
这里使用了isnull函数来判断是否缺失数据,并使用T属性转置使其按行输出。输出结果如下:
Country beer_servings spirit_servings wine_servings \
5 Antigua & Barbuda 102 128 45
11 Belize 263 114 8
14 Bolivia 167 41 8
17 Brazil 245 145 16
32 China 79 192 8
41 Czech Republic 361 170 134
43 Denmark 224 81 278
50 Falkland Islands beer_servings NaN 0
53 Finland 263 133 97
54 France 127 151 370
55 Gabon 347 98 59
64 Grenada 199 438 28
76 Italy 85 42 237
79 Japan 77 202 16
83 South Korea 140 16 9
87 Libya 0 0 0
88 North Korea 0 0 0
98 St. Kitts & Nevis 194 205 32
99 St. Lucia 171 315 71
109 Tanzania 36 6 1
110 Thailand 99 258 1
116 United States 249 158 84
126 Venezuela 333 100 3
...
total_litres_of_pure_alcohol Continent
5 4.9 North America
11 6.8 North America
14 3.8 South America
17 7.2 South America
32 5.0 Asia
41 11.8 Europe
43 10.4 Europe
50 8.8 South America
53 10.0 Europe
54 11.8 Europe
55 8.9 Africa
64 11.9 North America
76 6.5 Europe
79 7.0 Asia
83 9.8 Asia
87 0.0 Africa
88 0.0 Asia
98 7.7 North America
99 10.1 North America
109 5.7 Africa
110 6.4 Asia
116 8.7 North America
126 7.7 South America
可以看到,有一些国家的数据存在缺失值。
- 将NA替换为字符串NA
continent字段中的“NA”(代表北美洲,North American)自动识别为NaN,需要将其全部替换为字符串NA。可以使用fillna函数将空值替换。
df['Continent'].fillna('NA', inplace=True)
这里使用了inplace=True参数,直接修改原DataFrame中的数据。
- 输出各大洲的平均啤酒、烈酒和红酒的消费量
可以使用groupby函数按大洲分组,并计算各大洲的平均啤酒、烈酒和红酒的消费量。
grouped = df.groupby('Continent').mean()[['beer_servings', 'spirit_servings', 'wine_servings']] print(grouped)
输出结果如下:
beer_servings spirit_servings wine_servings
Continent
Africa 61.471698 16.339623 16.264151
Asia 37.045455 60.840909 9.068182
Europe 193.777778 132.555556 142.222222
NA 145.434783 165.739130 24.521739
Oceania 89.687500 58.437500 35.625000
South America 175.083333 114.750000 62.416667
- 输出啤酒、烈酒和红酒消费量最高的国家
可以使用nlargest函数找出啤酒、烈酒和红酒消费量最高的国家。
beer_max = df.nlargest(1, 'beer_servings') spirit_max = df.nlargest(1, 'spirit_servings') wine_max = df.nlargest(1, 'wine_servings')
print('啤酒消费量最高的国家:\n', beer_max[['Country', 'beer_servings']]) print('烈酒消费量最高的国家:\n', spirit_max[['Country', 'spirit_servings']]) print('红酒消费量最高的国家:\n', wine_max[['Country', 'wine_servings']])
输出结果如下:
啤酒消费量最高的国家: Country beer_servings 117 Namibia 376 烈酒消费量最高的国家: Country spirit_servings 68 Russian Federation 326 红酒消费量最高的国家: Country wine_servings 61 Portugal 339
四.画图
我们可以使用matplotlib库画图来可视化数据。下面以大洲的啤酒消费量为例。
首先,我们需要安装matplotlib库。
pip install matplotlib
然后,我们可以使用bar函数画出柱状图。
import matplotlib.pyplot as plt
plt.bar(grouped.index, grouped['beer_servings']) plt.xlabel('Continent') plt.ylabel('Beer Servings') plt.title('Beer Consumption by Continent') plt.show()
输出结果如下
原文地址: https://www.cveoy.top/t/topic/hvkR 著作权归作者所有。请勿转载和采集!