40 Pandas怎样实现groupby聚合后字符串列的合并
需求:
计算每个月的最高温度、最低温度、出现的风向列表、出现的空气质量列表
数据输入
数据输出
读取数据
import pandas as pd
fpath = "./datas/beijing_tianqi/beijing_tianqi_2018.csv"
df = pd.read_csv(fpath)
df.head(3)
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
|
ymd |
bWendu |
yWendu |
tianqi |
fengxiang |
fengli |
aqi |
aqiInfo |
aqiLevel |
0 |
2018-01-01 |
3℃ |
-6℃ |
晴~多云 |
东北风 |
1-2级 |
59 |
良 |
2 |
1 |
2018-01-02 |
2℃ |
-5℃ |
阴~多云 |
东北风 |
1-2级 |
49 |
优 |
1 |
2 |
2018-01-03 |
2℃ |
-5℃ |
多云 |
北风 |
1-2级 |
28 |
优 |
1 |
知识:使用df.info()可以查看每列的类型
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 365 entries, 0 to 364
Data columns (total 9 columns):
--- ------ -------------- -----
0 ymd 365 non-null object
1 bWendu 365 non-null object
2 yWendu 365 non-null object
3 tianqi 365 non-null object
4 fengxiang 365 non-null object
5 fengli 365 non-null object
6 aqi 365 non-null int64
7 aqiInfo 365 non-null object
8 aqiLevel 365 non-null int64
dtypes: int64(2), object(7)
memory usage: 25.8+ KB
知识:series怎样从str类型变成int
df["bWendu"] = df["bWendu"].str.replace("℃", "").astype('int32')
df["yWendu"] = df["yWendu"].str.replace("℃", "").astype('int32')
df.head(3)
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
|
ymd |
bWendu |
yWendu |
tianqi |
fengxiang |
fengli |
aqi |
aqiInfo |
aqiLevel |
0 |
2018-01-01 |
3 |
-6 |
晴~多云 |
东北风 |
1-2级 |
59 |
良 |
2 |
1 |
2018-01-02 |
2 |
-5 |
阴~多云 |
东北风 |
1-2级 |
49 |
优 |
1 |
2 |
2018-01-03 |
2 |
-5 |
多云 |
北风 |
1-2级 |
28 |
优 |
1 |
知识:进行日期列解析,可以方便提取月份
df["ymd"] = pd.to_datetime(df["ymd"])
df["ymd"].dt.month
0 1
1 1
2 1
3 1
4 1
..
360 12
361 12
362 12
363 12
364 12
Name: ymd, Length: 365, dtype: int64
知识:series可以用Series.unique去重
df["fengxiang"].unique()
array(['东北风', '北风', '西北风', '西南风', '南风', '东南风', '东风', '西风'], dtype=object)
知识:可以用",".join(series)实现数组合并成大字符串
",".join(df["fengxiang"].unique())
'东北风,北风,西北风,西南风,南风,东南风,东风,西风'
方法1
result = (
df.groupby(df["ymd"].dt.month)
.agg(
最高温度=("bWendu", "max"),
最低温度=("yWendu", "min"),
风向列表=("fengxiang", lambda x : ",".join(x.unique())),
空气质量列表=("aqiInfo", lambda x : ",".join(x.unique()))
)
.reset_index()
.rename(columns={"ymd":"月份"})
)
result
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
|
月份 |
最高温度 |
最低温度 |
风向列表 |
空气质量列表 |
0 |
1 |
7 |
-12 |
东北风,北风,西北风,西南风,南风,东南风,东风 |
良,优,轻度污染,中度污染 |
1 |
2 |
12 |
-10 |
北风,西南风,南风,西北风,西风,东北风,东风 |
良,优,轻度污染,中度污染,重度污染 |
2 |
3 |
27 |
-4 |
西南风,北风,东南风,南风,东北风,东风 |
优,良,重度污染,轻度污染,中度污染,严重污染 |
3 |
4 |
30 |
1 |
南风,北风,东北风,西南风,西北风,东南风 |
重度污染,良,优,轻度污染,中度污染 |
4 |
5 |
35 |
10 |
东北风,北风,西南风,南风,东南风,东风,西风,西北风 |
轻度污染,优,良,中度污染 |
5 |
6 |
38 |
17 |
西南风,南风,北风,东风,东南风,东北风 |
良,轻度污染,优,中度污染 |
6 |
7 |
37 |
22 |
东南风,西南风,南风,东北风,东风,西风,北风 |
良,轻度污染,优 |
7 |
8 |
36 |
20 |
东南风,南风,东风,东北风,北风,西南风 |
良,轻度污染,优 |
8 |
9 |
31 |
11 |
南风,北风,西南风,西北风 |
优,良,轻度污染 |
9 |
10 |
25 |
1 |
北风,西北风,南风,西风,东北风,西南风 |
优,良,轻度污染,中度污染 |
10 |
11 |
18 |
-4 |
南风,北风,西南风,东南风,西北风,东北风 |
良,轻度污染,重度污染,优,中度污染 |
11 |
12 |
10 |
-12 |
东南风,东北风,西北风,西南风 |
中度污染,重度污染,良,优,轻度污染 |
方法2
def agg_func(x):
"""注意,这个x是每个分组的dataframe"""
return pd.Series({
"最高温度": x["bWendu"].max(),
"最低温度": x["yWendu"].min(),
"风向列表": ",".join(x["fengxiang"].unique()),
"空气质量列表": ",".join(x["aqiInfo"].unique())
})
result = df \
.groupby(df["ymd"].dt.month) \
.apply(agg_func) \
.reset_index() \
.rename(columns={"ymd":"月份"})
result
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
|
月份 |
最高温度 |
最低温度 |
风向列表 |
空气质量列表 |
0 |
1 |
7 |
-12 |
东北风,北风,西北风,西南风,南风,东南风,东风 |
良,优,轻度污染,中度污染 |
1 |
2 |
12 |
-10 |
北风,西南风,南风,西北风,西风,东北风,东风 |
良,优,轻度污染,中度污染,重度污染 |
2 |
3 |
27 |
-4 |
西南风,北风,东南风,南风,东北风,东风 |
优,良,重度污染,轻度污染,中度污染,严重污染 |
3 |
4 |
30 |
1 |
南风,北风,东北风,西南风,西北风,东南风 |
重度污染,良,优,轻度污染,中度污染 |
4 |
5 |
35 |
10 |
东北风,北风,西南风,南风,东南风,东风,西风,西北风 |
轻度污染,优,良,中度污染 |
5 |
6 |
38 |
17 |
西南风,南风,北风,东风,东南风,东北风 |
良,轻度污染,优,中度污染 |
6 |
7 |
37 |
22 |
东南风,西南风,南风,东北风,东风,西风,北风 |
良,轻度污染,优 |
7 |
8 |
36 |
20 |
东南风,南风,东风,东北风,北风,西南风 |
良,轻度污染,优 |
8 |
9 |
31 |
11 |
南风,北风,西南风,西北风 |
优,良,轻度污染 |
9 |
10 |
25 |
1 |
北风,西北风,南风,西风,东北风,西南风 |
优,良,轻度污染,中度污染 |
10 |
11 |
18 |
-4 |
南风,北风,西南风,东南风,西北风,东北风 |
良,轻度污染,重度污染,优,中度污染 |
11 |
12 |
10 |
-12 |
东南风,东北风,西北风,西南风 |
中度污染,重度污染,良,优,轻度污染 |
本文使用 文章同步助手 同步