pandasgroupby函数
pandas groupby函数
pandas的groupby函数⼀般会配合合计函数使⽤,⽐如,count、avg等等。
⾸先讲解⼏种模式,然后⽰例上场:
第⼀种:df.groupby(col),返回⼀个按列进⾏分组的groupby对象;
第⼆种:df.groupby([col1,col2]),返回⼀个按多列进⾏分组的groupby对象;
第三种:df.groupby(col1)[col2]或者df[col2].groupby(col1),两者含义相同,返回按列col1进⾏分组后,col2的值;
先创建⼀个DataFrame对象df:
import pandasas pd
from dateutil.parserimport parse
import datetimeas dt
import matplotlib.pyplotas plt
df= pd.DataFrame({'Date': ['2015-05-08','2015-05-07','2015-05-06','2015-05-05','2015-05-08','2015-05-07','2015-05-06','2015-05-05'],
'Sym': ['aapl','aapl','aapl','aapl','aaww','aaww','aaww','aaww'],
'Data2': [11,8,10,15,110,60,100,40],
'Data3': [5,8,6,1,50,100,60,120]})
创建⼀个DataFrame对象df
结果数据
第⼀种:df.groupby(col),返回⼀个按列进⾏分组的groupby对象;
对⽇期进⾏分组,打印出结果样式,结果如下:
import pandasas pd
from dateutil.parserimport parse
import datetimeas dt
import matplotlib.pyplotas plt
df= pd.DataFrame({'Date': ['2015-05-08','2015-05-07','2015-05-06','2015-05-05','2015-05-08','2015-05-07','2015-05-06','2015-05-05'],
'Sym': ['aapl','aapl','aapl','aapl','aapl','aapl','aaww','aaww'],
'Data2': [11,8,10,15,110,60,100,40],
'Data3': [5,8,6,1,50,100,60,120]})
upby(df['Date'])
print(list(df1))
第⼆种:df.groupby([col1,col2]),返回⼀个按多列进⾏分组的groupby对象;
import pandasas pd
from dateutil.parserimport parse
import datetimeas dt
import matplotlib.pyplotas plt
df= pd.DataFrame({'Date': ['2015-05-08','2015-05-07','2015-05-06','2015-05-05','2015-05-08','2015-05-07','2015-05-06','2015-05-05'],
'Sym': ['aapl','aapl','aapl','aapl','aapl','aapl','aaww','aaww'],
'Data2': [11,8,10,15,110,60,100,40],
'Data3': [5,8,6,1,50,100,60,120]})
upby(['Date','Sym'])
print(list(df1))
第三种:df.groupby(col1)[col2]或者df[col2].groupby(col1),两者含义相同,返回按列col1进⾏分组后,col2的值;
import pandasas pd
from dateutil.parserimport parse
import datetimeas dt
import matplotlib.pyplotas pltgroupby是什么函数
df= pd.DataFrame({'Date': ['2015-05-08','2015-05-07','2015-05-06','2015-05-05','2015-05-08','2015-05-07','2015-05-06','2015-05-05'],
'Sym': ['aapl','aapl','aapl','aapl','aapl','aapl','aaww','aaww'],
'Data2': [11,8,10,15,110,60,100,40],
'Data3': [5,8,6,1,50,100,60,120]})
upby(df['Date'])['Sym']
print(list(df1))
import pandasas pd
from dateutil.parserimport parse
import datetimeas dt
import matplotlib.pyplotas plt
df= pd.DataFrame({'Date': ['2015-05-08','2015-05-07','2015-05-06','2015-05-05','2015-05-08','2015-05-07','2015-05-06','2015-05-05'],
'Sym': ['aapl','aapl','aapl','aapl','aapl','aapl','aaww','aaww'],
'Data2': [11,8,10,15,110,60,100,40],
'Data3': [5,8,6,1,50,100,60,120]})
df1=df['Sym'].groupby(df['Date'])
print(list(df1))
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论