最近做一个系列博客,跟着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

  1. >>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
  2. >>> df.columns = ['a', 'b']
  3. >>> df
  4. # a b
  5. #0 1 10
  6. #1 2 20

上面的方法直接给columns属性赋值, 如果需要对单个列名进行修改,可以

  1. col_names = df.columns.values
  2. col_names[0] = 'new_name'
  3. df.columns = col_names

但是绝对不能 df.columns.values[0] = 'new_name', df.columns.values 是不允许修改的。

方法2

如果仅对特定的列进行重命名,我们可以采用rename函数,进行操作。

  1. df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
  2. # OR
  3. new_df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
  4. # inplace = True 目的是修改原有Dataframe,不生成新的 DataFrame

参考

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename.html

  1. # rename 可以修改 Series 的index值
  2. >>> s = pd.Series([1, 2, 3])
  3. >>> s
  4. 0 1
  5. 1 2
  6. 2 3
  7. dtype: int64
  8. >>> s.rename("my_name") # scalar, changes Series.name
  9. 0 1
  10. 1 2
  11. 2 3
  12. Name: my_name, dtype: int64
  13. >>> s.rename(lambda x: x ** 2) # function, changes labels
  14. 0 1
  15. 1 2
  16. 4 3
  17. dtype: int64
  18. >>> s.rename({1: 3, 2: 5}) # mapping, changes labels
  19. 0 1
  20. 3 2
  21. 5 3
  22. dtype: int64
  23. >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
  24. >>> df.rename(2)
  25. Traceback (most recent call last):
  26. ...
  27. TypeError: 'int' object is not callable
  28. >>> df.rename(index=str, columns={"A": "a", "B": "c"})
  29. a c
  30. 0 1 4
  31. 1 2 5
  32. 2 3 6
  33. >>> df.rename(index=str, columns={"A": "a", "C": "c"})
  34. a B
  35. 0 1 4
  36. 1 2 5
  37. 2 3 6

【跟着stackoverflow学Pandas】Renaming columns in pandas-列的重命名的更多相关文章

  1. 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  2. 【跟着stackoverflow学Pandas】 -Get list from pandas DataFrame column headers - Pandas 获取列名

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  3. 【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  4. 【跟着stackoverflow学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  5. 【跟着stackoverflow学Pandas】Delete column from pandas DataFrame-删除列

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  6. 【跟着stackoverflow学Pandas】How to iterate over rows in a DataFrame in Pandas-DataFrame按行迭代

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  7. 【跟着stackoverflow学Pandas】“Large data” work flows using pandas-pandas大数据处理流程

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  8. pandas教程1:pandas数据结构入门

    pandas是一个用于进行python科学计算的常用库,包含高级的数据结构和精巧的工具,使得在Python中处理数据非常快速和简单.pandas建造在NumPy之上,它使得以NumPy为中心的应用很容 ...

  9. pandas取dataframe特定行/列

    1. 按列取.按索引/行取.按特定行列取 import numpy as np from pandas import DataFrame import pandas as pd df=DataFram ...

随机推荐

  1. 20145310 《Java程序设计》第7周学习总结

    20145310 <Java程序设计>第7周学习总结 教材学习内容总结 本周主要进行第十二章和第十三章的学习. Lambda定义:一个不用被绑定到一个标识符上,并且可能被调用的函数. 在只 ...

  2. linux c开发环境构成

    1.编辑器:VI.VIM 2.编译器:GNU C/C++编译器gcc 3.调试器:gdb 4.函数库:glibc 5.系统头文件:glibc_header

  3. LeetCode——Is Subsequence

    Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...

  4. js 苹果手机 keyup 事件不生效的问题

    $(document).on('keyup','input[name="txtInp"]',function(){}) 改 $(document).on('input proper ...

  5. linux:将job放在后台执行的方法

    本文转自http://www.ibm.com/developerworks/cn/linux/l-cn-nohup/ 我自己在工作需要远程连到公司的开发机,在开发机上运行程序时,一旦退出终端就会导致运 ...

  6. Extjs 分页多选的实现

    Extjs 版本 6.X 单页面的多选,没有任何问题. 直接使用 Grid的配置项进行绑定即可获取: xtype: 'grid', bind: { selection: '{checkedRecord ...

  7. 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 ...

  8. jquery validate检验

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. ubuntu16.04 安装power shell

    ubuntu16.04 安装power shell # Download the Microsoft repository GPG keys wget -q https://packages.micr ...

  10. Qwt中鼠标获取坐标点

    void getPoint(QwtPlot *plot) { QPoint point = plot->canvas()->mapFromGlobal(QCursor::pos()); Q ...