当前位置: 首页 > 文档资料 > Pandas 官方教程 >

Pandas 秘籍 - 第六章

优质
小牛编辑
120浏览
2023-12-01
  1. import pandas as pd
  2. pd.set_option('display.mpl_style', 'default')
  3. figsize(15, 3)

我们前面看到,Pandas 真的很善于处理日期。 它也善于处理字符串! 我们从第 5 章回顾我们的天气数据。

  1. weather_2012 = pd.read_csv('../data/weather_2012.csv', parse_dates=True, index_col='Date/Time')
  2. weather_2012[:5]
Temp (C)Dew Point Temp (C)Rel Hum (%)Wind Spd (km/h)Visibility (km)Stn Press (kPa)Weather
Date/Time
2012-01-01 00:00:00-1.8-3.98648.0101.24Fog
2012-01-01 01:00:00-1.8-3.78748.0101.24Fog
2012-01-01 02:00:00-1.8-3.48974.0101.26Freezing Drizzle,Fog
2012-01-01 03:00:00-1.5-3.28864.0101.27Freezing Drizzle,Fog
2012-01-01 04:00:00-1.5-3.38874.8101.23Fog

6.1 字符串操作

您会看到Weather列会显示每小时发生的天气的文字说明。 如果文本描述包含Snow,我们将假设它是下雪的。

pandas 提供了向量化的字符串函数,以便于对包含文本的列进行操作。 文档中有一些很好的例子。

  1. weather_description = weather_2012['Weather']
  2. is_snowing = weather_description.str.contains('Snow')

这会给我们一个二进制向量,很难看出里面的东西,所以我们绘制它:

  1. # Not super useful
  2. is_snowing[:5]
  1. Date/Time
  2. 2012-01-01 00:00:00 False
  3. 2012-01-01 01:00:00 False
  4. 2012-01-01 02:00:00 False
  5. 2012-01-01 03:00:00 False
  6. 2012-01-01 04:00:00 False
  7. Name: Weather, dtype: bool
  1. # More useful!
  2. is_snowing.plot()
  1. <matplotlib.axes.AxesSubplot at 0x403c190>

第六章 - 图1

6.2 使用resample找到下雪最多的月份

如果我们想要每个月的温度中值,我们可以使用resample()方法,如下所示:

  1. weather_2012['Temp (C)'].resample('M', how=np.median).plot(kind='bar')
  1. <matplotlib.axes.AxesSubplot at 0x560cc50>

第六章 - 图2

毫无奇怪,七月和八月是最暖和的。

所以我们可以将is_snowing转化为一堆 0 和 1,而不是TrueFalse

  1. Date/Time
  2. 2012-01-01 00:00:00 0
  3. 2012-01-01 01:00:00 0
  4. 2012-01-01 02:00:00 0
  5. 2012-01-01 03:00:00 0
  6. 2012-01-01 04:00:00 0
  7. 2012-01-01 05:00:00 0
  8. 2012-01-01 06:00:00 0
  9. 2012-01-01 07:00:00 0
  10. 2012-01-01 08:00:00 0
  11. 2012-01-01 09:00:00 0
  12. Name: Weather, dtype: float64

然后使用resample寻找每个月下雪的时间比例。

  1. is_snowing.astype(float).resample('M', how=np.mean)
  1. Date/Time
  2. 2012-01-31 0.240591
  3. 2012-02-29 0.162356
  4. 2012-03-31 0.087366
  5. 2012-04-30 0.015278
  6. 2012-05-31 0.000000
  7. 2012-06-30 0.000000
  8. 2012-07-31 0.000000
  9. 2012-08-31 0.000000
  10. 2012-09-30 0.000000
  11. 2012-10-31 0.000000
  12. 2012-11-30 0.038889
  13. 2012-12-31 0.251344
  14. Freq: M, dtype: float64
  1. is_snowing.astype(float).resample('M', how=np.mean).plot(kind='bar')
  1. <matplotlib.axes.AxesSubplot at 0x5bdedd0>

第六章 - 图3

所以现在我们知道了! 2012 年 12 月是下雪最多的一个月。 此外,这个图表暗示着我感觉到的东西 - 11 月突然开始下雪,然后慢慢变慢,需要很长时间停止,最后下雪的月份通常在 4 月或 5 月。

6.3 将温度和降雪绘制在一起

我们还可以将这两个统计(温度和降雪)合并为一个DataFrame,并将它们绘制在一起:

  1. temperature = weather_2012['Temp (C)'].resample('M', how=np.median)
  2. is_snowing = weather_2012['Weather'].str.contains('Snow')
  3. snowiness = is_snowing.astype(float).resample('M', how=np.mean)
  4. # Name the columns
  5. temperature.name = "Temperature"
  6. snowiness.name = "Snowiness"

我们再次使用concat,将两个统计连接为一个DataFrame

  1. stats = pd.concat([temperature, snowiness], axis=1)
  2. stats
TemperatureSnowiness
Date/Time
2012-01-31-7.050.240591
2012-02-29-4.100.162356
2012-03-312.600.087366
2012-04-306.300.015278
2012-05-3116.050.000000
2012-06-3019.600.000000
2012-07-3122.900.000000
2012-08-3122.200.000000
2012-09-3016.100.000000
2012-10-3111.300.000000
2012-11-301.050.038889
2012-12-31-2.850.251344
  1. stats.plot(kind='bar')
  1. <matplotlib.axes.AxesSubplot at 0x5f59d50>

第六章 - 图4

这并不能正常工作,因为比例不对,我们可以在两个图表中分别绘制它们,这样会更好:

  1. stats.plot(kind='bar', subplots=True, figsize=(15, 10))
  1. array([<matplotlib.axes.AxesSubplot object at 0x5fbc150>,
  2. <matplotlib.axes.AxesSubplot object at 0x60ea0d0>], dtype=object)

第六章 - 图5