首页 > 解决方案 > 转换后如何更新数据框的列?

问题描述

我有以下数据框

      age  sex   cp 
0    63.0  1.0  1.0
1    67.0  1.0  4.0
2    41.0  0.0  2.0

我对每一列应用了转换过程,如下所示:

age = store_data['age']
age_bins = [0, 40, 60, 100]
age_categories = pd.cut(age, age_bins)

sex = store_data['sex']
sex_series = pd.Series(sex, dtype = "category")
sex_rename = sex_series.cat.rename_categories(['F','M'])


cp = store_data['cp']
cp_series = pd.Series(cp, dtype = "category")
cp_rename = cp_series.cat.rename_categories(["typical","atypical","non-anginal","asymptomatic"])

每个的输出如下所示:

>>age_categories
0      (60, 100]
1      (60, 100]
2       (40, 60]

>>sex_rename
0      M
1      M
4      F

>>cp_rename
0           typical
1      asymptomatic
2          atypical

如何使用新的转换值更新原始列:age_categories、sex_rename、cp_rename?我想保留旧名称(年龄,性别,cp)作为头部

标签: pythonpandastransformationcutcat

解决方案


尝试消除额外的变量?我没有运行它,因为没有数据,但这应该直接更新您的数据框。

age_bins = [0, 40, 60, 100]
store_data['age'] = pd.cut(store_data['age'], age_bins)

__

store_data['sex'] = pd.Series(store_data['sex'], dtype = "category").cat.rename_categories(['F','M'])

__

store_data['cp'] = pd.Series(store_data['cp'], dtype = "category").cat.rename_categories(["typical","atypical","non-anginal","asymptomatic"])

推荐阅读