gpt4 book ai didi

python - 转换后如何更新数据框的列?

转载 作者:太空宇宙 更新时间:2023-11-04 11:20:44 25 4
gpt4 key购买 nike

我有以下数据框

      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)作为头部

最佳答案

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

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"])

关于python - 转换后如何更新数据框的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030629/

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