我转换了这个R代码:
# Raw data
data <- data.frame(
metalname=c('Al','Cd','Cr','Co','Cu','Au','Fe','Pb','Mo','Ni','Pt','Au','Ta','Ti','W','Zn'),
radius=c(0.1431,0.1490,0.1249,0.1253,0.1278,0.1442,0.1241,0.1750,0.1363,0.1246,0.1387,0.1445,0.1430,0.1445,0.1371,0.1332),
crystal=c('FCC','HCP','BCC','HCP','FCC','FCC','BCC','FCC','BCC','FCC','FCC','FCC','BCC','HCP','BCC','HCP'))
# Calc lattice parameters (nm)
data <- rbind(
transform(subset(data, crystal=='BCC'), N=2, latticea=4*radius/sqrt(3), latticec=0),
transform(subset(data, crystal=='FCC'), N=4, latticea=2*radius*sqrt(2), latticec=0),
transform(subset(data, crystal=='HCP'), N=6, latticea=2*radius, latticec=4*radius*sqrt(2/3))
)
这个熊猫代码:
import pandas as pd
import numpy as np
import math
from pandas import DataFrame
# Raw data
data = DataFrame({
'metalname': ['Al','Cd','Cr','Co','Cu','Au','Fe','Pb','Mo','Ni','Pt','Au','Ta','Ti','W','Zn'],
'radius': [0.1431,0.1490,0.1249,0.1253,0.1278,0.1442,0.1241,0.1750,0.1363,0.1246,0.1387,0.1445,0.1430,0.1445,0.1371,0.1332],
'crystal': ['FCC','HCP','BCC','HCP','FCC','FCC','BCC','FCC','BCC','FCC','FCC','FCC','BCC','HCP','BCC','HCP']
})
# Calc lattice parameters (nm)
databcc = data[data.crystal=='BCC']
databcc['N'] = 2
databcc['latticea'] = 4*databcc.radius/math.sqrt(3)
datafcc = data[data.crystal=='FCC']
datafcc['N'] = 4
datafcc['latticea'] = 2*datafcc.radius/math.sqrt(2)
datahcp = data[data.crystal=='HCP']
datahcp['N'] = 6
datahcp['latticea'] = 2*datahcp.radius
datahcp['latticec'] = 4*datahcp.radius*math.sqrt(2/3)
data = databcc.append(datafcc).append(datahcp)
代码有效,但有没有办法让Python版本更简洁?理想情况下,我可以在没有R代码等临时变量的情况下一步完成多列计算。这可能吗?
答案 0 :(得分:4)
这是pandas 0.13中新的query
/ eval
功能的用例
databcc = data.query('crystal == "BCC"')
sqrt3 = sqrt(3)
databcc.eval('latticea = 4 * radius / sqrt3')
# ...
目前无法在表达式字符串中调用函数,因此您必须定义局部变量并使用它。
答案 1 :(得分:3)
这将是非常快的全部矢量化
In [65]: data.join(
concat([
DataFrame(dict(N=2, latticea=4*data.loc[data.crystal=='BCC','radius']/np.sqrt(3))),
DataFrame(dict(N=4, latticea=2*data.loc[data.crystal=='FCC','radius']/np.sqrt(2))),
DataFrame(dict(N=6, latticea=2*data.loc[data.crystal=='HCP','radius'],
latticec=4*data.loc[data.crystal=='HCP','radius']/np.sqrt(2/3.0)))
]))
Out[65]:
crystal metalname radius N latticea latticec
0 FCC Al 0.1431 4 0.202374 NaN
1 HCP Cd 0.1490 6 0.298000 0.729948
2 BCC Cr 0.1249 2 0.288444 NaN
3 HCP Co 0.1253 6 0.250600 0.613842
4 FCC Cu 0.1278 4 0.180736 NaN
5 FCC Au 0.1442 4 0.203930 NaN
6 BCC Fe 0.1241 2 0.286597 NaN
7 FCC Pb 0.1750 4 0.247487 NaN
8 BCC Mo 0.1363 2 0.314771 NaN
9 FCC Ni 0.1246 4 0.176211 NaN
10 FCC Pt 0.1387 4 0.196151 NaN
11 FCC Au 0.1445 4 0.204354 NaN
12 BCC Ta 0.1430 2 0.330244 NaN
13 HCP Ti 0.1445 6 0.289000 0.707903
14 BCC W 0.1371 2 0.316619 NaN
15 HCP Zn 0.1332 6 0.266400 0.652544
[16 rows x 6 columns]
答案 2 :(得分:1)
这相当于原始问题代码看起来比原始R杰作更好:
import pdb
import pandas as pd
import numpy as np
import math
from pandas import DataFrame
# Raw data
data = DataFrame({
'metalname': ['Al','Cd','Cr','Co','Cu','Au','Fe','Pb','Mo','Ni','Pt','Au','Ta','Ti','W','Zn'],
'radius': [0.1431,0.1490,0.1249,0.1253,0.1278,0.1442,0.1241,0.1750,0.1363,0.1246,0.1387,0.1445,0.1430,0.1445,0.1371,0.1332],
'crystal': ['FCC','HCP','BCC','HCP','FCC','FCC','BCC','FCC','BCC','FCC','FCC','FCC','BCC','HCP','BCC','HCP']
})
def calc_lattic_params(x):
N = None
l = None
lc = None
if x['crystal'] == 'BCC':
N = 2
l = 4 * x['radius'] / math.sqrt(3)
elif x['crystal'] == 'FCC':
N = 4
l = 2*x['radius'] / math.sqrt(2)
elif x['crystal'] == 'HCP':
N = 6
l = 2*x['radius']
lc = 4*x['radius']*math.sqrt(2.0/3.0)
return pd.Series({'N': N, 'latticea': l, 'latticec': lc})
data = pd.concat([data, data.apply(calc_lattic_params, axis = 1)], axis = 1)
答案 3 :(得分:1)
如果有人感兴趣的话,可以使用Incanter(基于Lisp)版本进行比较:
(use '(incanter core stats charts))
; Raw data
(def data (dataset [:metalname :radius :crystal] [
["Al" 0.1431 "FCC"]
["Cd" 0.1490 "HCP"]
["Cr" 0.1249 "BCC"]
["Co" 0.1253 "HCP"]
["Cu" 0.1278 "FCC"]
["Au" 0.1442 "FCC"]
["Fe" 0.1241 "BCC"]
["Pb" 0.1750 "FCC"]
["Mo" 0.1363 "BCC"]
["Ni" 0.1246 "FCC"]
["Pt" 0.1387 "FCC"]
["Au" 0.1445 "FCC"]
["Ta" 0.1430 "BCC"]
["Ti" 0.1445 "HCP"]
["W" 0.1371 "BCC"]
["Zn" 0.1332 "HCP"]
]))
; Calc lattice parameters (nm)
(conj-rows
(add-derived-column :latticec [] (fn [] 0)
(add-derived-column :latticea [:radius] (fn [r] (/ (* 4 r) (sqrt 3)))
(add-derived-column :n [] (fn [] 2)
($where {:crystal "BCC"} data))))
(add-derived-column :latticec [] (fn [] 0)
(add-derived-column :latticea [:radius] (fn [r] (* 2 r (sqrt 2)))
(add-derived-column :n [] (fn [] 4)
($where {:crystal "FCC"} data))))
(add-derived-column :latticec [:radius] (fn [r] (* 4 r (sqrt (/ 2 3))))
(add-derived-column :latticea [:radius] (fn [r] (* 2 r))
(add-derived-column :n [] (fn [] 6)
($where {:crystal "HCP"} data)))))
答案 4 :(得分:1)
由于问题是代码简洁性(Python
vs R
)的比较,这里是使用R
的{{1}}解决方案:
data.table
设置library(data.table)
dt <- data.table(data, key="crystal")
data_transformed_dt <- rbind(dt["BCC", .(metalname, radius, crystal, N=2, latticea=4*radius/sqrt(3), latticec=0)],
dt['FCC', .(metalname, radius, crystal, N=4, latticea=2*radius*sqrt(2), latticec=0)],
dt['HCP', .(metalname, radius, crystal, N=6, latticea=2*radius, latticec=4*radius*sqrt(2/3))])
的优点是它为水晶列编制索引,就像数据库中的索引一样。如果数据集非常大,这将极大地节省搜索时间(key="crystal"
,"BCC"
...)。
另一种方法是创建另一个键 data.table,如下所示:
"FCC"
然后我们可以在加入时更新原始# v1.9.5+, for new feature "on = ", See github project page
require(data.table)
key = data.table(crystal = c("BCC", "FCC", "HCP"),
latticea = c(4/sqrt(3), 2*sqrt(2), 2),
latticec=c(0,0,4*sqrt(2/3)),
N = c(2,4,6))
,如下所示:
data
这应该是非常高效和快速的内存,因为我们通过引用更新(并且没有实现整个连接)。 setDT(data)[key , c("latticea", "latticec", "N") :=
.(radius * latticea, radius * latticec, N),
on = "crystal"]
# metalname radius crystal latticea latticec N
# 1: Al 0.1431 FCC 0.4047479 0.0000000 4
# 2: Cd 0.1490 HCP 0.2980000 0.4866320 6
# 3: Cr 0.1249 BCC 0.2884442 0.0000000 2
# 4: Co 0.1253 HCP 0.2506000 0.4092281 6
# 5: Cu 0.1278 FCC 0.3614730 0.0000000 4
# 6: Au 0.1442 FCC 0.4078592 0.0000000 4
# 7: Fe 0.1241 BCC 0.2865967 0.0000000 2
# 8: Pb 0.1750 FCC 0.4949747 0.0000000 4
# 9: Mo 0.1363 BCC 0.3147714 0.0000000 2
# 10: Ni 0.1246 FCC 0.3524220 0.0000000 4
# 11: Pt 0.1387 FCC 0.3923028 0.0000000 4
# 12: Au 0.1445 FCC 0.4087077 0.0000000 4
# 13: Ta 0.1430 BCC 0.3302444 0.0000000 2
# 14: Ti 0.1445 HCP 0.2890000 0.4719350 6
# 15: W 0.1371 BCC 0.3166189 0.0000000 2
# 16: Zn 0.1332 HCP 0.2664000 0.4350294 6
在该列上执行连接,并在on = "crystal"
中找到与data
中每一行对应的匹配行索引,在这些匹配的行上,我们同时更新/创建必要的列。
请注意,数据的原始顺序也会保留在结果中。