gpt4 book ai didi

python - pandas 列划分 ValueError (putmask : mask and data must be the same size)

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:51 24 4
gpt4 key购买 nike

我试图在函数内部将一列除以另一列:

lcontrib=lcontrib_lev.div(lcontrib_lev['base'],axis='index')

可以看出,我在 DataFrame 中除以一列,但出现了一个相当奇怪的错误:

ValueError: putmask: mask and data must be the same size

我必须承认,这是我第一次看到这种错误。似乎表明 DF 和列的长度不同,但显然(因为列来自 DataFrame)它们不是。

另一个转折是,我使用此函数在特定年份的集合上循环数据管理过程(数据来自 Quarterly Census of Employment and Wages 中的“单个文件” beta series)。与 1990-2000 时间段相关联的集顺利运行,但 2001 年抛出此错误。恐怕多年来我一直无法识别出结构上的差异,即使我可以,又如何解释长度不匹配?

如有任何想法,我们将不胜感激。

编辑(2014 年 2 月 1 日):感谢 Tom 的关注。根据要求,pandas 版本为 0.13.0,相关数据文件位于 here在 BLS FTP 站点上。只是为了澄清我所说的一致结构的意思,每年都有相同的变量集和数据类型(除了一致的数据代码结构)。

编辑(2014 年 2 月 1 日):也许共享整个功能会很有用:

def qcew(f,m_dict):
'''Function reads in file and captures county level aggregations with government contributions'''
#Read in file
cew=pd.read_csv(f)

#Create string version of area fips
cew['fips']=cew['area_fips'].astype(str)

#Generate description variables
cew['area']=cew['fips'].map(m_dict['area'])
cew['industry']=cew['industry_code'].map(m_dict['industry'])
cew['agglvl']=cew['agglvl_code'].map(m_dict['agglvl'])
cew['own']=cew['own_code'].map(m_dict['ownership'])
cew['size']=cew['size_code'].map(m_dict['size'])

#Generate boolean masks
lagg_mask=cew['agglvl_code']==73
lsize_mask=cew['size_code']==0

#Subset data to above specifications
cew_super=cew[lagg_mask & lsize_mask]

#Define column subset
lsub_cols=['year','fips','area','industry_code','industry','own','annual_avg_estabs_count','annual_avg_emplvl',\
'total_annual_wages','own_code']

#Subset to desired columns
cew_sub=cew_super[lsub_cols]

#Rename columns
cew_sub.columns=['year','fips','cty','ind_code','industry','own','estabs','emp','tot_wages','own_code']

#Set index
cew_sub.set_index(['year','fips','cty'],inplace=True)

#Capture total wage base and the contributions of Federal, State, and Local
cew_base=cew_sub['tot_wages'].groupby(level=['year','fips','cty']).sum()
cew_fed=cew_sub[cew_sub['own_code']==1]['tot_wages'].groupby(level=['year','fips','cty']).sum()
cew_st=cew_sub[cew_sub['own_code']==2]['tot_wages'].groupby(level=['year','fips','cty']).sum()
cew_loc=cew_sub[cew_sub['own_code']==3]['tot_wages'].groupby(level=['year','fips','cty']).sum()

#Convert to DFs for join
lbase=DataFrame(cew_base).rename(columns={0:'base'})
lfed=DataFrame(cew_fed).rename(columns={0:'fed_wage'})
lstate=DataFrame(cew_st).rename(columns={0:'st_wage'})
llocal=DataFrame(cew_loc).rename(columns={0:'loc_wage'})

#Join these series
lcontrib_lev=pd.concat([lbase,lfed,lstate,llocal],axis='index').fillna(0)

#Diag prints
print f
print lcontrib_lev.head()
print lcontrib_lev.describe()
print '*****************************\n'

#Calculate proportional contributions (failure point)
lcontrib=lcontrib_lev.div(lcontrib_lev['base'],axis='index')

#Group base data by year, county, and industry
cew_g=cew_sub.reset_index().groupby(['year','fips','cty','ind_code','industry']).sum().reset_index()

#Join contributions to joined data
cew_contr=cew_g.set_index(['year','fips','cty']).join(lcontrib[['fed_wage','st_wage','loc_wage']])

return cew_contr[[x for x in cew_contr.columns if x != 'own_code']]

最佳答案

对我来说工作正常(这是在 0.13.1 上,但 IIRC 我不认为这个特定区域有任何改变,但它可能是一个已修复的错误)。

In [48]: lcontrib_lev.div(lcontrib_lev['base'],axis='index').head()
Out[48]:
base fed_wage st_wage loc_wage
year fips cty
2001 1000 1000 NaN NaN NaN NaN
1000 NaN NaN NaN NaN
10000 10000 NaN NaN NaN NaN
10000 NaN NaN NaN NaN
10001 10001 NaN NaN NaN NaN

[5 rows x 4 columns]

In [49]: lcontrib_lev.div(lcontrib_lev['base'],axis='index').tail()
Out[49]:
base fed_wage st_wage loc_wage
year fips cty
2001 CS566 CS566 1 0.000000 0.000000 0.000000
US000 US000 1 0.022673 0.027978 0.073828
USCMS USCMS 1 0.000000 0.000000 0.000000
USMSA USMSA 1 0.000000 0.000000 0.000000
USNMS USNMS 1 0.000000 0.000000 0.000000

[5 rows x 4 columns]

关于python - pandas 列划分 ValueError (putmask : mask and data must be the same size),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21513659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com