数据的读取

1
2
3
# 导入相应的库
import numpy as np
import pandas as pd
1
2
# 导入数据集
df = pd.read_csv('https://raw.githubusercontent.com/xiryg/blog_picture/main/resource/data(%E9%80%9A%E5%B7%9E).csv')
1
2
# 查看数据集形状
df.shape #(行,列)
(107, 5)
1
2
# 迅速了解数据的结构
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 107 entries, 0 to 106
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   时间      107 non-null    object
 1   天气      107 non-null    object
 2   温度      107 non-null    object
 3   风向      107 non-null    object
 4   风级      107 non-null    object
dtypes: object(5)
memory usage: 4.3+ KB
1
2
# 快速查看数据的描述性统计信息
df.describe()
时间天气温度风向风级
count107107107107107
unique10771074
top15日23时多云27℃东南风<3级
freq139264980

行的选择— 三种方法选取前5行

1
df.head(5)     # head(参数)   选取前几行
时间天气温度风向风级
015日23时中雨26℃东南风<3级
116日02时暴雨26℃东风<3级
216日05时暴雨26℃东南风<3级
316日08时中雨27℃东南风<3级
416日11时小雨30℃北风<3级
1
df[:5]  # 选取0-9行
时间天气温度风向风级
015日23时中雨26℃东南风<3级
116日02时暴雨26℃东风<3级
216日05时暴雨26℃东南风<3级
316日08时中雨27℃东南风<3级
416日11时小雨30℃北风<3级
1
df.iloc[:5,:]   # iloc 隐式索引  iloc[行操作,列操作]
时间天气温度风向风级
015日23时中雨26℃东南风<3级
116日02时暴雨26℃东风<3级
216日05时暴雨26℃东南风<3级
316日08时中雨27℃东南风<3级
416日11时小雨30℃北风<3级
1
df.loc[:4,:]  # loc 显式索引   loc[行操作,列操作]
时间天气温度风向风级
015日23时中雨26℃东南风<3级
116日02时暴雨26℃东风<3级
216日05时暴雨26℃东南风<3级
316日08时中雨27℃东南风<3级
416日11时小雨30℃北风<3级

列的选择—三种方法选取前2列

1
df[['时间','天气']].head(3)
时间天气
015日23时中雨
116日02时暴雨
216日05时暴雨
1
df.loc[:,['时间','天气']].head(2)
时间天气
015日23时中雨
116日02时暴雨
1
df.iloc[:,:2].head(2)
时间天气
015日23时中雨
116日02时暴雨

区域快的选择—三种方法选取前三行,前二列

1
df.iloc[:3,:2]  # 选取前3行,前2列
时间天气
015日23时中雨
116日02时暴雨
216日05时暴雨
1
df.loc[0:2,['时间','天气']]
时间天气
015日23时中雨
116日02时暴雨
216日05时暴雨
1
df.loc[:,['时间','天气']].head(3)
时间天气
015日23时中雨
116日02时暴雨
216日05时暴雨

增加分析的条件,选取温度 >=30℃

使用字符串相关操作替换℃符号并转换数据类型

1
df['温度'] = df['温度'].str.replace('℃', '').astype(int)
1
df.head(2)
时间天气温度风向风级
015日23时中雨26东南风<3级
116日02时暴雨26东风<3级

使用正则表达式替换℃符号并转换数据类型

1
2
import re
df = pd.read_csv('https://raw.githubusercontent.com/xiryg/blog_picture/main/resource/data(%E9%80%9A%E5%B7%9E).csv')
1
df['温度'] = df['温度'].apply(lambda x: re.sub(r'℃', '', x)).astype(int)
1
df.head(2)
时间天气温度风向风级
015日23时中雨26东南风<3级
116日02时暴雨26东风<3级
1
df['温度'] >= 30   # 返回bool值类型
0      False
1      False
2      False
3      False
4       True
       ...  
102    False
103    False
104    False
105    False
106    False
Name: 温度, Length: 107, dtype: bool
1
df[df['温度'] >= 30].head(5)   # 取得温度>=30的数据集
时间天气温度风向风级
416日11时小雨30北风<3级
1217日11时小雨30东南风<3级
2018日11时小雨31东风<3级
2118日14时小雨32东北风<3级
2218日17时小雨30东风<3级
1
2
3
# 将“时间”设置为索引
df1 = df.set_index('时间')
df1.head(2)
天气温度风向风级
时间
15日23时中雨26东南风<3级
16日02时暴雨26东风<3级
1
2
3
 # 设置“天气” “温度”为索引
df2 = df.set_index(['天气','温度'])
df2.head(2)
时间风向风级
天气温度
中雨2615日23时东南风<3级
暴雨2616日02时东风<3级
1
2
3
# Method 1 删除 风向和风级两列
ch_df = df.copy()
ch_df.head()
时间天气温度风向风级
015日23时中雨26东南风<3级
116日02时暴雨26东风<3级
216日05时暴雨26东南风<3级
316日08时中雨27东南风<3级
416日11时小雨30北风<3级
1
del ch_df['风向']
1
del ch_df['风级']
1
ch_df.head(2)
时间天气温度
015日23时中雨26
116日02时暴雨26
1
2
# Method 2
df_ch = df.drop(['风向','风级'],axis=1)
1
df_ch.head(2)
时间天气温度
015日23时中雨26
116日02时暴雨26

数据的分组

1
df.head(2)
时间天气温度风向风级
015日23时中雨26东南风<3级
116日02时暴雨26东风<3级
1
2
# 查看天气值的数量
df['天气'].value_counts()
多云    39
小雨    27
中雨    18
阴     14
暴雨     3
大雨     3
晴      3
Name: 天气, dtype: int64
1
2
# 能看到上述有7种天气类型
len(df['天气'].value_counts())
7
1
2
# 或者使用nunique()
df['天气'].nunique()
7

将所需要的字段作为索引,然后使用汇总函数进行汇总计算

1
df.groupby('天气').mean()
C:\Users\86183\AppData\Local\Temp\ipykernel_8120\2078587693.py:1: FutureWarning: The default value of numeric_only in DataFrameGroupBy.mean is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function.
  df.groupby('天气').mean()
温度
天气
中雨28.000000
多云28.769231
大雨28.000000
小雨28.444444
28.666667
暴雨26.000000
27.785714
1
df.groupby('天气').mean(numeric_only=True) # 根据天气进行分组,mean()对可计算类有效
温度
天气
中雨28.000000
多云28.769231
大雨28.000000
小雨28.444444
28.666667
暴雨26.000000
27.785714
1
df.groupby('天气')['温度'].mean()
天气
中雨    28.000000
多云    28.769231
大雨    28.000000
小雨    28.444444
晴     28.666667
暴雨    26.000000
阴     27.785714
Name: 温度, dtype: float64
1
df.groupby('天气').size() # 返回分组个数
天气
中雨    18
多云    39
大雨     3
小雨    27
晴      3
暴雨     3
阴     14
dtype: int64
1
df.groupby(['天气','风向']).size().head(5)
天气  风向 
中雨  东北风    3
    东南风    9
    东风     3
    南风     1
    西南风    2
dtype: int64
1
df.groupby('天气').get_group('晴')   # 以“天气”分组,取得“晴”数据
时间天气温度风向风级
6724日08时31南风<3级
7224日23时27东南风<3级
8726日20时28东南风<3级
1
df.groupby('天气').get_group('中雨').head(3)
时间天气温度风向风级
015日23时中雨26东南风<3级
316日08时中雨27东南风<3级
716日20时中雨25东风<3级

总结

数据集获取,输入地址即可

Jupyter notebook 交互比较好

loc 使用标签(label)进行索引,而 iloc 使用整数位置(integer position)进行索引。

也就是 loc (显式)iloc (隐式)

  • loc:通过标签(label)进行索引。它接受的参数是行和列的标签名称。例如,df.loc[row_label, column_label]loc 是基于标签的索引,包括起始和终止位置。

  • iloc:通过整数位置(integer position)进行索引。它接受的参数是行和列的整数位置。例如,df.iloc[row_index, column_index]iloc 是基于整数位置的索引,从 0 开始计数。

从前面的:

1
df.loc[:,['时间','天气']].head(2)
1
df.iloc[:,:2].head(2)

产生的效果是一样的,应该可以理解 lociloc 的区别

1
df['温度'] = df['温度'].str.replace('℃', '').astype(int)
  • df['温度']:选中 温度
  • .str.replace('℃', ''):对选定的列使用 str.replace() 方法,将字符串中的 ‘℃’ 替换为空字符串。
  • .astype(int):将处理后的字符串数据转换为整数类型。使用 astype() 方法,并传入 int 参数,将字符串转换为整数。
1
df['温度'] = df['温度'].apply(lambda x: re.sub(r'℃', '', x)).astype(int)
  • apply() 方法,传入匿名函数 使用 re.sub 方法 通过正则表达式完成转换。

以上就是数据的读取与分组相关内容

笔记 备忘 随时看


春江潮水连海平,海上明月共潮生。