【跟着stackoverflow学Pandas】Renaming columns in pandas-列的重命名
最近做一个系列博客,跟着stackoverflow学Pandas。
以 pandas
作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序:
https://stackoverflow.com/questions/tagged/pandas?sort=votes&pageSize=15
Renaming columns in pandas - 列的重命名
https://stackoverflow.com/questions/11346283/renaming-columns-in-pandas
方法1
>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df.columns = ['a', 'b']
>>> df
# a b
#0 1 10
#1 2 20
上面的方法直接给columns属性赋值, 如果需要对单个列名进行修改,可以
col_names = df.columns.values
col_names[0] = 'new_name'
df.columns = col_names
但是绝对不能 df.columns.values[0] = 'new_name'
, df.columns.values 是不允许修改的。
方法2
如果仅对特定的列进行重命名,我们可以采用rename
函数,进行操作。
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# OR
new_df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
# inplace = True 目的是修改原有Dataframe,不生成新的 DataFrame
参考
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename.html
# rename 可以修改 Series 的index值
>>> s = pd.Series([1, 2, 3])
>>> s
0 1
1 2
2 3
dtype: int64
>>> s.rename("my_name") # scalar, changes Series.name
0 1
1 2
2 3
Name: my_name, dtype: int64
>>> s.rename(lambda x: x ** 2) # function, changes labels
0 1
1 2
4 3
dtype: int64
>>> s.rename({1: 3, 2: 5}) # mapping, changes labels
0 1
3 2
5 3
dtype: int64
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.rename(2)
Traceback (most recent call last):
...
TypeError: 'int' object is not callable
>>> df.rename(index=str, columns={"A": "a", "B": "c"})
a c
0 1 4
1 2 5
2 3 6
>>> df.rename(index=str, columns={"A": "a", "C": "c"})
a B
0 1 4
1 2 5
2 3 6
【跟着stackoverflow学Pandas】Renaming columns in pandas-列的重命名的更多相关文章
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】 -Get list from pandas DataFrame column headers - Pandas 获取列名
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Delete column from pandas DataFrame-删除列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】How to iterate over rows in a DataFrame in Pandas-DataFrame按行迭代
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】“Large data” work flows using pandas-pandas大数据处理流程
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- pandas教程1:pandas数据结构入门
pandas是一个用于进行python科学计算的常用库,包含高级的数据结构和精巧的工具,使得在Python中处理数据非常快速和简单.pandas建造在NumPy之上,它使得以NumPy为中心的应用很容 ...
- pandas取dataframe特定行/列
1. 按列取.按索引/行取.按特定行列取 import numpy as np from pandas import DataFrame import pandas as pd df=DataFram ...
随机推荐
- 20145310 《Java程序设计》第7周学习总结
20145310 <Java程序设计>第7周学习总结 教材学习内容总结 本周主要进行第十二章和第十三章的学习. Lambda定义:一个不用被绑定到一个标识符上,并且可能被调用的函数. 在只 ...
- linux c开发环境构成
1.编辑器:VI.VIM 2.编译器:GNU C/C++编译器gcc 3.调试器:gdb 4.函数库:glibc 5.系统头文件:glibc_header
- LeetCode——Is Subsequence
Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...
- js 苹果手机 keyup 事件不生效的问题
$(document).on('keyup','input[name="txtInp"]',function(){}) 改 $(document).on('input proper ...
- linux:将job放在后台执行的方法
本文转自http://www.ibm.com/developerworks/cn/linux/l-cn-nohup/ 我自己在工作需要远程连到公司的开发机,在开发机上运行程序时,一旦退出终端就会导致运 ...
- Extjs 分页多选的实现
Extjs 版本 6.X 单页面的多选,没有任何问题. 直接使用 Grid的配置项进行绑定即可获取: xtype: 'grid', bind: { selection: '{checkedRecord ...
- Educational Codeforces Round 13 A、B、C、D
A. Johny Likes Numbers time limit per test 0.5 seconds memory limit per test 256 megabytes input sta ...
- jquery validate检验
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- ubuntu16.04 安装power shell
ubuntu16.04 安装power shell # Download the Microsoft repository GPG keys wget -q https://packages.micr ...
- Qwt中鼠标获取坐标点
void getPoint(QwtPlot *plot) { QPoint point = plot->canvas()->mapFromGlobal(QCursor::pos()); Q ...