尝试计算Python Pandas的百分比
#q-numbers > div:first-child {
font-weight: bold;
font-size: 1em;
margin: 10px 10px;
}
#q-numbers .table {
display: table;
width: 100%;
border-spacing: 10px;
}
#q-numbers .table > div {
display: table-row;
}
#q-numbers .table > div > div {
display: table-cell;
width: 50%;
position: relative;
}
#q-numbers .table > div > div > div {
padding: 4px 0px;
}
#q-numbers .blue,
#q-numbers .orange {
background-color: #ccc;
border-top: 1px solid #707376;
border-bottom: 1px solid #707376;
height: 80px;
}
#q-numbers .square span {
height: auto;
border-spacing: 10px;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%) scale(1, 1);
transform: translate(-50%, -50%) scale(1, 1);
-webkit-transform: translate(-50%, -50%) scale(1, 1);
font-size: 3.5vw;
}
#q-numbers .footer {
background-color: #eee;
text-align: center;
padding: 5px 0px;
}
A和B的百分比输出得分高于0.
$query = "SELECT name FROM labs WHERE name LIKE '$text%'";
答案 0 :(得分:3)
(df['2'] > 0)
)sum
和size
汇总(总和将计算符合条件的汇总数)res = (df['2'] > 0).groupby(df['1']).agg(['sum', 'size'])
res['sum'] / res['size']
Out:
1
A 0.666667
B 0.250000
dtype: float64
这可以通过lambda表达式以更紧凑的方式完成:
df.groupby('1')['2'].agg(lambda x: (x > 0).sum() / x.size)
Out:
1
A 0.666667
B 0.250000
Name: 2, dtype: float64
但我怀疑第一个效率更高。
答案 1 :(得分:3)
In [3]: df['2'].gt(0).groupby(df['1']).mean()
Out[3]:
1
A 0.666667
B 0.250000
Name: 2, dtype: float64
答案 2 :(得分:3)
别介意我...
我是on a kick我用np.bincount
和pd.factorize
f, u = df['1'].factorize()
pd.Series(
np.bincount(f, df['2'].values > 0) / np.bincount(f),
u
)
A 0.666667
B 0.250000
dtype: float64
单行版本的乐趣!
(lambda w, g, f, u: pd.Series(g(f, w) / g(f)))(
df['2'].values > 0, np.bincount, *pd.factorize(df['1'].values)
)
天真时间
%timeit df['2'].gt(0).groupby(df['1']).mean()
%timeit df.groupby('1')['2'].agg(lambda x: (x > 0).sum() / x.size)
%timeit (lambda w, g, f, u: pd.Series(g(f, w) / g(f)))(df['2'].values > 0, np.bincount, *pd.factorize(df['1'].values))
1000 loops, best of 3: 697 µs per loop
1000 loops, best of 3: 1 ms per loop
10000 loops, best of 3: 117 µs per loop