pandas修改DataFrame中的列名&调整列的顺序

以你之姓@ 2023-06-16 04:52 92阅读 0赞

修改列名:

直接调用接口:

  1. df.rename()

看一下接口中的定义:

  1. def rename(self, *args, **kwargs):
  2. """
  3. Alter axes labels.
  4. Function / dict values must be unique (1-to-1). Labels not contained in
  5. a dict / Series will be left as-is. Extra labels listed don't throw an
  6. error.
  7. See the :ref:`user guide <basics.rename>` for more.
  8. Parameters
  9. ----------
  10. mapper, index, columns : dict-like or function, optional
  11. dict-like or functions transformations to apply to
  12. that axis' values. Use either ``mapper`` and ``axis`` to
  13. specify the axis to target with ``mapper``, or ``index`` and
  14. ``columns``.
  15. axis : int or str, optional
  16. Axis to target with ``mapper``. Can be either the axis name
  17. ('index', 'columns') or number (0, 1). The default is 'index'.
  18. copy : boolean, default True
  19. Also copy underlying data
  20. inplace : boolean, default False
  21. Whether to return a new DataFrame. If True then value of copy is
  22. ignored.
  23. level : int or level name, default None
  24. In case of a MultiIndex, only rename labels in the specified
  25. level.
  26. Returns
  27. -------
  28. renamed : DataFrame
  29. See Also
  30. --------
  31. pandas.DataFrame.rename_axis
  32. Examples
  33. --------
  34. ``DataFrame.rename`` supports two calling conventions
  35. * ``(index=index_mapper, columns=columns_mapper, ...)``
  36. * ``(mapper, axis={'index', 'columns'}, ...)``
  37. We *highly* recommend using keyword arguments to clarify your
  38. intent.
  39. >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
  40. >>> df.rename(index=str, columns={"A": "a", "B": "c"})
  41. a c
  42. 0 1 4
  43. 1 2 5
  44. 2 3 6
  45. >>> df.rename(index=str, columns={"A": "a", "C": "c"})
  46. a B
  47. 0 1 4
  48. 1 2 5
  49. 2 3 6
  50. Using axis-style parameters
  51. >>> df.rename(str.lower, axis='columns')
  52. a b
  53. 0 1 4
  54. 1 2 5
  55. 2 3 6
  56. >>> df.rename({1: 2, 2: 4}, axis='index')
  57. A B
  58. 0 1 4
  59. 2 2 5
  60. 4 3 6
  61. """
  62. axes = validate_axis_style_args(self, args, kwargs, 'mapper', 'rename')
  63. kwargs.update(axes)
  64. # Pop these, since the values are in `kwargs` under different names
  65. kwargs.pop('axis', None)
  66. kwargs.pop('mapper', None)
  67. return super(DataFrame, self).rename(**kwargs)

注意:

  • 一个*,输入可以是数组、元组,会把输入的数组或元组拆分成一个个元素。
  • 两个*,输入必须是字典格式

示例:

  1. >>>import pandas as pd
  2. >>>a = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6], 'C':[7,8,9]})
  3. >>> a
  4. A B C
  5. 0 1 4 7
  6. 1 2 5 8
  7. 2 3 6 9
  8. #将列名A替换为列名a,B改为b,C改为c
  9. >>>a.rename(columns={'A':'a', 'B':'b', 'C':'c'}, inplace = True)
  10. >>>a
  11. a b c
  12. 0 1 4 7
  13. 1 2 5 8
  14. 2 3 6 9

调整列的顺序:

如:

  1. >>> import pandas
  2. >>> dict_a = {'user_id':['webbang','webbang','webbang'],'book_id':['3713327','4074636','26873486'],'rating':['4','4','4'],
  3. 'mark_date':['2017-03-07','2017-03-07','2017-03-07']}
  4. >>> df = pandas.DataFrame(dict_a) # 从字典创建DataFrame
  5. >>> df # 创建好的df列名默认按首字母顺序排序,和字典中的先后顺序并不一样,字典中'user_id','book_id','rating','mark_date'
  6. book_id mark_date rating user_id
  7. 0 3713327 2017-03-07 4 webbang
  8. 1 4074636 2017-03-07 4 webbang
  9. 2 26873486 2017-03-07 4 webbang

直接修改列名:

  1. >>> df = df[['user_id','book_id','rating','mark_date']] # 调整列顺序为'user_id','book_id','rating','mark_date'
  2. >>> df
  3. user_id book_id rating mark_date
  4. 0 webbang 3713327 4 2017-03-07
  5. 1 webbang 4074636 4 2017-03-07
  6. 2 webbang 26873486 4 2017-03-07

就可以了。

发表评论

表情:
评论列表 (有 0 条评论,92人围观)

还没有评论,来说两句吧...

相关阅读