我有一个带有两列的df' a'和' b'
[a] [b]
11 100
2 100
10 100
我需要的是一个额外的列' c',它代表以下计算:
((11-2)+(11-10))/ 100
((2-11)+(2-10))/ 100
((10-11)+(10-2))/ 100
[a] [b] [c]
11 100 0.1
2 100 -0.17
10 100 0.07
它应该是高度动态的,因此[a]的行数可以不同。速度也是一个问题,这就是我想避免循环的原因。
我尝试使用.apply()和.pivot()以简单的格式来调用sub(),但它没有成功。
答案 0 :(得分:2)
我会给出一个笨拙的例子。对于
>>> a = numpy.array([11, 2, 10])
>>> b = numpy.array([100, 100, 100])
你可以做到
>>> c = (len(a) * a - sum(a)) / b
类似于pandas数据框。
答案 1 :(得分:2)
另一个解决方案是重写大熊猫:
使用mul
,sub
,
length
和div
,DataFrame
len(df.index)
df['c'] = df.a.mul(len(df.index)).sub(df.a.sum()).div(df.b)
print (df)
a b c
0 11 100 0.10
1 2 100 -0.17
2 10 100 0.07
最快((11-2) + (11-10))
(2 * 11) - (2 + 10)
它有效,因为:
(3 * 11) - (2 + 10 + 11)
与:
相同[Tue Feb 14 08:27:12.364749 2017] [:error] [pid 7587] [client 55.55.56.1:49446] PHP Fatal error: Uncaught PrestaShopException: Property Currency->decimals is not valid in /vagrant/prestashop/classes/ObjectModel.php:909
Stack trace:
#0 /vagrant/prestashop/classes/LocalizationPack.php(299): ObjectModelCore->validateFields()
#1 /vagrant/prestashop/classes/LocalizationPack.php(68): LocalizationPackCore->_installCurrencies(Object(SimpleXMLElement), true)
#2 /vagrant/prestashop/install/models/install.php(549): LocalizationPackCore->loadLocalisationPack('<?xml version="...', '', true)
#3 /vagrant/prestashop/install/controllers/http/process.php(207): InstallModelInstall->configureShop(Array)
#4 /vagrant/prestashop/install/controllers/http/process.php(93): InstallControllerHttpProcess->processConfigureShop()
#5 /vagrant/prestashop/install/classes/controllerHttp.php(173): InstallControllerHttpProcess->process()
#6 /vagrant/prestashop/install/index.php(31): InstallControllerHttp::execute()
#7 {main}
thrown in /vagrant/prestashop/classes/ObjectModel.php on line 909, referer: http://dev.imagine-lotus.design/install/index.php?restart=true
和以下相同:
if (strtolower($bride->photographer) === 'yes') {
// Code goes here
}