开帖目的:记录一些在写Python工作中经常遇到的一些问题和解决办法,以防遗忘。
开发环境:Ubuntu16.04+Python3
1. lambda
我们在coding过程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想花费精力去构造一个函数的场合下使用,通常也称作它是指匿名函数。
通过一个简单的例子了解其用法
def g1(x):
return float(x)
g2 = lambda x : float(x)
print (g1(6),g2(6))
>>> 6.0 6.0
我们看到g1与g2的功能一样
2.zip函数
迭代函数,日常工作使用频率还是很高的

3.数据统计量计算
import numpy as np
import stats as sts
scores = [31, 24, 23, 25, 14, 25, 13, 12, 14, 23,
32, 34, 43, 41, 21, 23, 26, 26, 34, 42,
43, 25, 24, 23, 24, 44, 23, 14, 52,32,
42, 44, 35, 28, 17, 21, 32, 42, 12, 34]
#集中趋势的度量
print('求和:',np.sum(scores))
print('个数:',len(scores))
print('平均值:',np.mean(scores))
print('中位数:',np.median(scores))
print('众数:',sts.mode(scores))
print('上四分位数',sts.quantile(scores,p=0.25))
print('下四分位数',sts.quantile(scores,p=0.75))
#离散趋势的度量
print('最大值:',np.max(scores))
print('最小值:',np.min(scores))
print('极差:',np.max(scores)-np.min(scores))
print('四分位差',sts.quantile(scores,p=0.75)-sts.quantile(scores,p=0.25))
print('标准差:',np.std(scores))
print('方差:',np.var(scores))
print('离散系数:',np.std(scores)/np.mean(scores))
#偏度与峰度的度量
print('偏度:',sts.skewness(scores))
print('峰度:',sts.kurtosis(scores))
python groupby 站总体百分比
import pandas as pd
import numpy as np
df = pd.DataFrame({'state': ['CA', 'WA', 'CO', 'AZ'] * 3,
'office_id':list(range(1,7)) * 2,
'sales': [np.random.randint(100000, 999999)
for _ in range(12)]})
df1=df.groupby(['state','office_id']).aggregate({'sales':np.sum})
df2=df1.groupby(level=0).apply(lambda x: x*100/float(x.sum()))

数据分析服务
可以私信、或者加微:一八五五六五二五六零八
待续…
261



被折叠的 条评论
为什么被折叠?



