Python KeyError:';A2142A2744A2199A119&x27;
我试图让“集群”一个接一个地读取下面数据帧的底部,但我无法做到这一点,我也尝试使用Python KeyError:';A2142A2744A2199A119&x27;,python,pandas,Python,Pandas,我试图让“集群”一个接一个地读取下面数据帧的底部,但我无法做到这一点,我也尝试使用for,但我仍然无法得到我所期望的。所以我尝试使用这个方法,我得到了这个错误。我希望你们能给出一些解决这个错误的方法 from scipy import constants from qconstants import * import os.path import pandas as pd cluster = 'A2142' cluster += 'A2744' cluster += 'A2199' clust
for
,但我仍然无法得到我所期望的。所以我尝试使用这个方法,我得到了这个错误。我希望你们能给出一些解决这个错误的方法
from scipy import constants
from qconstants import *
import os.path
import pandas as pd
cluster = 'A2142'
cluster += 'A2744'
cluster += 'A2199'
cluster += 'A119'
################ List of Cluster##################################################################
d1=pd.DataFrame.from_items([
('A2142' ,['A2142' ,'MH',0.0909 ,418600 ,23 ,129 ,0.57 ,0.810 ,5.82 ,10.0 ,0.5 ,6189.32 ,510 ,2000]),
('A2744' ,['A2744' ,'RH',0.3080 , 28840, 218, 115, 7.7, 0.680, 10.8, 10.0, 0.5, 920.667, 1360, 1100]),
('A2199' ,['A2199' ,'RH',0.0302 , 127000 ,250 , 99.3, 2.0, 0.655, 10.1, 11.7, 0.9, 14655.02, 180, 1000.0]),
('A119' ,['A119' ,'RH',0.0440 ,832000, 243 , 357.9, 5.8, 0.675, 0.15, 7.5, 0.9, 5627.63, 5770, 1100.0])],
orient='index',columns=['Label','type','redshift','DL','S_nu','r_c_dum','T_gas_dum','beta','n0','B_dum','eta','rho_s','r_s','r'])
############################# Set and decide the constants start ##################################################
#Hubble constants
h =0.7 #little h. unit : -
#Critical density:
sigm =0.3 #matter density. unit : -
sigl =0.7 #dark energy density. unit : -
sig8 =0.829
#Properties of cluster:
redshift =d1.loc[cluster,'redshift']
DL =d1.loc[cluster,'DL']*kpc*100.0 # luminosity distance, in unit of cm. z=0.0302
#DA =100000*kpc*100.0
S_nu =d1.loc[cluster,'S_nu'] #mJy
r_c_dum =d1.loc[cluster,'r_c_dum'] #Core radius. unit : h50^-1 kpc, where H0=50 kms^-1Mpc^-1
T_gas_dum =d1.loc[cluster,'T_gas_dum'] #T_gas in unit of keV
beta =d1.loc[cluster,'beta'] #Beta parameter. unit : -
n0 =d1.loc[cluster,'n0'] #Thermal electron central density #unit : cm^-3. A&A 540, A38 (2012)
B_dum =d1.loc[cluster,'B_dum'] #central magnetic field. #unit : Gauss. A&A 540, A38 (2012)
eta =d1.loc[cluster,'eta'] #proportional coefficient related to magnetic field profile
#Temperature of CMB at any redshift z. (want ot know more? See http://www.cv.nrao.edu/course/astr534/CMB.html)
T =T0*(1.0+redshift)
#conversion if necessary
B0 =B_dum*(10**-6)
H_0 =100.0*h #hubble constant. unit : km s^-1 Mpc^-1
H_0m =H_0/(1000.0*si_pc) #hubble constant. unit : s^-1
Ez =np.sqrt(sigm*(1.0+redshift)**3+sigl)
Hz =H_0m*Ez #H(z). unit : s^-1
rho_crit=3.0*Hz**2/(8.0*constants.pi*si_G) #critical density. unit : kg m^-3
T_gas =T_gas_dum*1000.0*si_e/si_k #T_gas in unit of Kelvin
r_c_dum2=r_c_dum*0.5/h #Core radius. unit : kpc, where h=0.673
r_c =r_c_dum2*1000.0*si_pc*100.0 #Core radius. unit : cm
############################# Set and decide the constants end ####################################################
############################# start: parameters for nfw profile ####################################################
#characteristic density in unit of kg m^-3 ( multiplied by rho_crit )
rho_s=d1.loc[cluster,'rho_s']*rho_crit
#characteristic radius in unit of meter
r_s= kpc*d1.loc[cluster,'r_s']
r=kpc*d1.loc[cluster,'r']
############################# end: parameters for nfw profile ####################################################```
这个问题写得不好 似乎您正在尝试对每行进行一些计算(您称为cluster)。 或者您正在尝试进行一些
N行计算
最终的预期结果还不清楚?什么是nfw配置文件
您是否正在尝试构建其他列?或者得到一些转换后的数据
下面是您使用df.apply()重新编写的代码,以执行每行计算以显示每行计算。(需要更多的清理工作
)
代码缺少几个项目,因此我将它们全部默认为1.0T0=1.0,si_pc=1.0,si_G=1.0,si_e=1.0,si_k=1.0,kpc=1.0,rho=1.0
创建数据帧示例
perRow
-每行应用的函数
将perRow()
应用于d1数据帧的每一行。
输出
from_items()在较新的Python中已被弃用。。。使用dict()。
import numpy as np
items= [
('A2142' ,['A2142' ,'MH',0.0909 ,418600 ,23 ,129 ,0.57 ,0.810 ,5.82 ,10.0 ,0.5 ,6189.32 ,510 ,2000]),
('A2744' ,['A2744' ,'RH',0.3080 , 28840, 218, 115, 7.7, 0.680, 10.8, 10.0, 0.5, 920.667, 1360, 1100]),
('A2199' ,['A2199' ,'RH',0.0302 , 127000 ,250 , 99.3, 2.0, 0.655, 10.1, 11.7, 0.9, 14655.02, 180, 1000.0]),
('A119' ,['A119' ,'RH',0.0440 ,832000, 243 , 357.9, 5.8, 0.675, 0.15, 7.5, 0.9, 5627.63, 5770, 1100.0])]
d1 = pd.DataFrame.from_dict({k:v for k,v in items},
orient='index',columns=['Label','type','redshift','DL','S_nu','r_c_dum','T_gas_dum','beta','n0','B_dum','eta','rho_s','r_s','r'])
def perRow(r, T0=1.0, si_pc = 1.0, si_G = 1.0, si_e = 1.0, si_k = 1.0, kpc=1.0, rho=1.0):
############################# Set and decide the constants start ##################################################
#Hubble constants
h =0.7 #little h. unit : -
#Critical density:
sigm =0.3 #matter density. unit : -
sigl =0.7 #dark energy density. unit : -
sig8 =0.829
#Properties of cluster:
#redshift = r.redshift
#DL = r.DL *kpc*100.0 # luminosity distance, in unit of cm. z=0.0302
#DA =100000*kpc*100.0
#S_nu =r.S_nu
#r_c_dum = r.r_c_dum #Core radius. unit : h50^-1 kpc, where H0=50 kms^-1Mpc^-1
#T_gas_dum = r.T_gas_dum #T_gas in unit of keV
#beta = r.beta #Beta parameter. unit : -
#n0 = r.n0 #Thermal electron central density #unit : cm^-3. A&A 540, A38 (2012)
#B_dum = r.B_dum #central magnetic field. #unit : Gauss. A&A 540, A38 (2012)
#eta = r.eta #proportional coefficient related to magnetic field profile
#Temperature of CMB at any redshift z. (want ot know more? See http://www.cv.nrao.edu/course/astr534/CMB.html)
T =T0*(1.0+r.redshift)
#conversion if necessary
B0 = r.B_dum*(10**-6)
H_0 =100.0*h #hubble constant. unit : km s^-1 Mpc^-1
H_0m =H_0/(1000.0*si_pc) #hubble constant. unit : s^-1
Ez =np.sqrt(sigm*(1.0+r.redshift)**3+sigl)
Hz =H_0m*Ez #H(z). unit : s^-1
# FIXME: used np.pi instead of constants.pi
rho_crit=3.0*Hz**2/(8.0*np.pi*si_G) #critical density. unit : kg m^-3
T_gas = r.T_gas_dum*1000.0*si_e/si_k #T_gas in unit of Kelvin
r_c_dum2= r.r_c_dum*0.5/h #Core radius. unit : kpc, where h=0.673
r_c = r_c_dum2*1000.0*si_pc*100.0 #Core radius. unit : cm
############################# Set and decide the constants end ####################################################
############################# start: parameters for nfw profile ####################################################
#characteristic density in unit of kg m^-3 ( multiplied by rho_crit )
rho_s= r.rho_s*rho_crit
#characteristic radius in unit of meter
r_s= kpc* r.r_s
r=kpc*r.r
############################# end: parameters for nfw profile ####################################################```
nfw_profile = [rho_s, r_s, r]
return(nfw_profile)
print("\n NFW PROFILE [rho_s, r_s, r]")
print(d1.apply(lambda r: perRow(r),axis=1))
NFW PROFILE [rho_s, r_s, r]
A2142 [3.943995655495191, 510.0, 2000.0]
A2744 [0.7384586065967704, 1360.0, 1100.0]
A2199 [8.811723258555253, 180.0, 1000.0]
A119 [3.427734855427434, 5770.0, 1100.0]
dtype: object