欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pandas groupby()的使用小結(jié)

 更新時間:2023年11月08日 09:03:08   作者:m0_38093796  
在數(shù)據(jù)分析中,經(jīng)常會用到分組,可用函數(shù)pandas中的groupby(),本文就來介紹一下pandas groupby()的使用小結(jié),具有一定的參考價值,感興趣的可以了解一下

在數(shù)據(jù)分析中,經(jīng)常會用到分組,可用函數(shù) pandas 中的groupby()。

groupby()主要的分組統(tǒng)計(jì)方向:

1、分組匯總
2、分組描述性統(tǒng)計(jì)分析
3、分組計(jì)算均值等統(tǒng)計(jì)量
4、分組去重
5、分組排序取樣本
6、分組匯總,然后再分組計(jì)算

Date	Latitude	Longitude	Depth	Type
01/02/1965	19.246	145.616	131.6	Earthquake
01/04/1965	1.863	127.352	80.0	Earthquake
01/05/1965	-20.579	-173.972	20.0	Earthquake
01/08/1965	-59.076	-23.557	15.0	Earthquake
01/09/1965	11.938	126.427	15.0	Earthquake
#1、分組統(tǒng)計(jì)組內(nèi)個數(shù)
df.groupby(['date','Type'])
	.size()

#2、分組統(tǒng)計(jì)計(jì)算‘Depth'的基本數(shù)據(jù)分布
df.groupby(['date','type'])['Depth']
	.describe()
	
#3、分組統(tǒng)計(jì)計(jì)算‘Latitude'的均值	
df.groupby(['date','type'])
	.agg({'Latitude':'mean'})

#分組統(tǒng)計(jì)計(jì)算兩列數(shù)據(jù)的均值
df.groupby(['date','type'])
	.agg(la_mean = ('Latitude','mean'),
 		 lo_mean = ('Longitude','mean'),
 		 la_range = ('depth',lambda x:x.max() - x.min()))

#4、分組然后對每組去重,然后匯總?cè)ブ睾蟮膫€數(shù)
df.groupby('date')
	.apply(lambda x: x.drop_duplicates('Type')['Type']
		.count())

#5、分組,然后按照‘Latitude'降序,并取出每組的前3個樣本。
df.groupby(['date'])
	.apply(lambda x: x.sort_values('Latitude',ascending = False)
			.head(3))

#6、分組,然后匯總計(jì)算兩列數(shù)據(jù),再進(jìn)行分組,取出每一組‘la_sum'的最大值
df.groupby(['date','type'])
	.agg(la_sum = ('Latitude','sum'),
		 lo_sum = ('Longitude','sum'))
		 	.groupby(['date'])
				.apply(lambda x:x.sort_values('la_sum ',ascending = False).head(1))

#分組,然后按照type排序,再分組,取出前10匯總和后10匯總,然后再計(jì)算兩者的比率
df.groupby('date')
	.apply(lambda x:x.sort_values('type'))
		.reset_index(drop = True).groupby('date')
			.agg(tail10 = ('depth',lambda x:x.head(10).sum()),
				 top10 = ('depth',lambda x:x.tail(10).sum()))
					.apply(lambda x:x['top10']/x['tail10'],axis = 1)

groupby()結(jié)合agg()和apply(),可以解決很多復(fù)雜的分組計(jì)算問題。

到此這篇關(guān)于pandas groupby()的使用小結(jié)的文章就介紹到這了,更多相關(guān)pandas groupby()使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論