Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError #1

Open
hungyangtsai opened this issue Sep 21, 2023 · 1 comment
Open

KeyError #1

hungyangtsai opened this issue Sep 21, 2023 · 1 comment

Comments

@hungyangtsai
Copy link

Hi, I used Weightipy to cpmpute my data's weight,the code in below:

`age_targets = {'agecat':{1: 0.067468889, 2: 0.080224503, 3: 0.081368672, 4: 0.084373472, 5: 0.102660891
,6:0.094806186,7:0.090030135,8:0.090639452,9:0.088830485,10:0.077339418,11:0.136466392,98:0.005791506}}

gender_targets = {'gender':{1: 0.488588237, 2: 0.511411763}}

county_targets = {"county":{2:0.019395905,4:0.023782735,5:0.02292341,7:0.02292341,8:0.02088535,9:0.028729086,10:0.021700082
,13:0.034964756,14:0.009158398,15:0.013730506,16:0.004724007,17:0.015997925,18:0.018345312,20:0.011139324
,63:0.107256094,64:0.11836086,65:0.173914542,66:0.118862683,67:0.080211099,68:0.095929136,72:0.006447622}}

scheme = wp.Rim('gender_and_age_and_county')
scheme.set_targets(targets=[age_targets, gender_targets,county_targets])

pres_df["identity"] = range(len(pres_df))
engine = wp.WeightEngine(data=pres_df)
engine.add_scheme(scheme=scheme, key="identity", verbose=False)
engine.run()
df_weighted = engine.dataframe()
col_weights = f"weights_{scheme.name}"

efficiency = wp.weighting_efficiency(df_weighted[col_weights])
`
when run this code,show KeyError: "['gender', 'agecat', 'county'] not in index",how do I to fix it? Thanks!

@MDS-JAnthony
Copy link

Hi,
This normally suggests that those columns do not exist in your data.
The first place to check is that "gender", "agecat" and "county" are columns in your pres_df.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants