英文:
Changing data frame values to corresponding numbers
问题
I'm trying to take data that appears like this.
age_gender | page_fans_gender_age | new_age_group | new_gender_group |
---|---|---|---|
F.13-17 | 1259 | 13-17 | F |
F.18-24 | 3329 | 18-24 | F |
F.25-34 | 14090 | 25-34 | F |
F.35-44 | 5864 | 35-44 | F |
F.45-54 | 3619 | 45-54 | F |
M.13-17 | 3847 | 13-17 | M |
M.18-24 | 57721 | 18-24 | M |
M.25-34 | 59493 | 25-34 | M |
M.35-44 | 21751 | 35-44 | M |
M.45-54 | 9417 | 45-54 | M |
M.55-64 | 5721 | 55-64 | M |
M.65+ | 104 | 65+ | M |
U.35-44 | 1770 | 35-44 | U |
when printed to the console and change the age groups(13-17 etc) to a corresponding number and gender (F or M etc) to a corresponding number through a loop with else if statements inside.
When I try to change given values to a corresponding number I get a key error which seems to refer to pandas not being able to find the column. But just above where I try and change it I make the new columns as the data comes in as F.18-24 or M.18-24 etc. So I make the columns by df_pivot_table['new_age_group'] = df_pivot_table.index.str[2:7]
to separate the age group.
Now when I loop over the data in order to change say any 13-17 to 0 so Highcharts can use that as an x axis, I get a key error of '13-17' or 'F' if I'm doing gender group. From the looks of it pandas is thinking 'F' is a column when it's just a value in one of the rows. Any help on how to fix this would be huge
for i in df_pivot_table['new_gender_group']:
if df_pivot_table['new_gender_group'][i] == 'F':
print('female gender group')
I've tried making df_pivot_table['new_gender_group']
to a string by using .astype(str)
, reassigning the values to a new variable in order to separate the values in hopes that that I could track down exactly what's happening easier, changing these values after I have the final list that is about to be sent to highcharts as they're then in a list of arrays.
英文:
I'm trying to take data that appears like this.
age_gender | page_fans_gender_age | new_age_group | new_gender_group |
---|---|---|---|
F.13-17 | 1259 | 13-17 | F |
F.18-24 | 3329 | 18-24 | F |
F.25-34 | 14090 | 25-34 | F |
F.35-44 | 5864 | 35-44 | F |
F.45-54 | 3619 | 45-54 | F |
M.13-17 | 3847 | 13-17 | M |
M.18-24 | 57721 | 18-24 | M |
M.25-34 | 59493 | 25-34 | M |
M.35-44 | 21751 | 35-44 | M |
M.45-54 | 9417 | 45-54 | M |
M.55-64 | 5721 | 55-64 | M |
M.65+ | 104 | 65+ | M |
U.35-44 | 1770 | 35-44 | U |
when printed to the console and change the age groups(13-17 etc) to a corresponding number and gender (F or M etc) to a corresponding number through a loop with else if statements inside.
When I try to change given values to a corresponding number I get a key error which seems to refer to pandas not being able to find the column. But just above where I try and change it I make the new columns as the data comes in as F.18-24 or M.18-24 etc. So I make the columns by
df_pivot_table['new_age_group'] = df_pivot_table.index.str[2:7]
to separate the age group.
Now when I loop over the data in order to change say any 13-17 to 0 so Highcharts can use that as an x axis, I get a key error of '13-17' or 'F' if I'm doing gender group. From the looks of it pandas is thinking 'F' is a column when it's just a value in one of the rows. Any help on how to fix this would be huge
for i in df_pivot_table['new_gender_group']:
if df_pivot_table['new_gender_group'][i] == 'F':
print('female gender group')
I've tried making df_pivot_table['new_gender_group']
to a string by using .astype(str)
, reassigning the values to a new variable in order to separate the values in hopes that that I could track down exactly what's happening easier, changing these values after I have the final list that is about to be sent to highcharts as they're then in a list of arrays.
答案1
得分: 1
要更改列值,只需运行以下代码:df_pivot_table['new_gender_group'] = df_pivot_table['new_gender_group'].replace('M', 0) 以将M替换为0。
英文:
In order to change the column value I just needed to run df_pivot_table['new_gender_group'] = df_pivot_table['new_gender_group'].replace('M', 0) in order to replace M with 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论