当我像这样应用函数时 中位数= Top15 ['%可再生']。中位数(轴= 0)
def func(Top15):
if (Top15['% Renewable'] >= median):
Top15['HighRenew'] = 1
else:
Top15['HighRenew'] = 0
return Top15
Top15.apply(func,axis=1)
答案 0 :(得分:2)
首先我无法模拟你的问题。
我认为更好的是与布尔掩码相比较,并将int
True
转换为1
False
和0
转换为Top15['Rank'] = (Top15['% Renewable'] >= Top15['% Renewable'].median(axis=0)).astype(int)
:< / p>
apply
避免Top15 = pd.DataFrame({'% Renewable':[10,23,56,78,90],
'Rank':[10,20,30,4,50]})
print (Top15)
#Top15 = pd.concat([Top15] * 1000, ignore_index=True)
% Renewable Rank
0 10 10
1 23 20
2 56 30
3 78 4
4 90 50
median = Top15['% Renewable'].median(axis=0)
def func(x):
if (x['% Renewable'] >= median):
x['HighRenew'] = 1
else:
x['HighRenew'] = 0
return x
Top15 = Top15.apply(func,axis=1)
Top15['Rank2'] = (Top15['% Renewable'] >= Top15['% Renewable'].median(axis=0)).astype(int)
print (Top15)
% Renewable Rank HighRenew Rank2
0 10 10 0 0
1 23 20 0 0
2 56 30 1 1
3 78 4 1 1
4 90 50 1 1
(如果可能)的主要原因是引擎盖下的循环。
<强>示例强>:
Top15 = pd.DataFrame({'% Renewable':[10,23,56,78,90],
'Rank':[10,20,30,4,50]})
print (Top15)
Top15 = pd.concat([Top15] * 1000, ignore_index=True)
In [49]: %timeit Top15.apply(func,axis=1)
1 loop, best of 3: 595 ms per loop
In [50]: %timeit (Top15['% Renewable'] >= Top15['% Renewable'].median(axis=0)).astype(int)
The slowest run took 5.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 346 µs per loop
<强>计时强>:
public static void sendResponse (InputStream inputData, String fileName, HttpServletResponse res)
throws IOException {
try {
String contenttype = new ConfigurableMimeFileTypeMap().getContentType(fileName);
res.reset();
res.setContentType(contenttype);
res.addHeader("Content-Disposition", String.format("attachment;filename=\"%s\"", name));
ByteStreams.copy(inputData, res.getOutputStream());
} catch (Exception e) {
throw new IOException(e);
}
}
答案 1 :(得分:0)
你可以使用astype TOP15 [ '等级'] = Top15.Rank.astype(int)的 要么 TOP15 [ '等级'] = Top15.Rank.astype(对象)