python - 转换后如何更新数据框的列?
问题描述
我有以下数据框
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"])
推荐阅读
- c++ - 如何从 Windows 任务管理器或 C++ 中的任何其他内容获取进程的 GPU 和 CPU 使用率?
- c++ - 当我使用连接信号和插槽时发生此部署错误
- python - 如何从一个函数访问另一个函数?
- pygame - 如何计算圆和矩形之间的最小平移向量?
- c# - 如何根据匹配搜索在 JArray 中找到对象索引?
- javascript - 在javascript中创建没有循环的范围的总和
- python - 分离因变量和自变量
- laravel-5 - 在第 82 行 Laravel 上没有状态或消息的 VerifyCsrfToken 错误
- android - 从 Firestore 集合中获取所有文档和子集合
- c# - 如何安全地从 InkPresenter.StrokesCollected 事件中打开一个线程?调试 && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION