广告
返回顶部
首页 > 资讯 > 后端开发 > Python >pandas调整列的顺序以及添加列的实现
  • 211
分享到

pandas调整列的顺序以及添加列的实现

2024-04-02 19:04:59 211人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

在对excel的操作中,调整列的顺序以及添加一些列也是经常用到的,下面我们用pandas实现这一功能。 1、调整列的顺序 >>> df = pd.read_ex

在对excel的操作中,调整列的顺序以及添加一些列也是经常用到的,下面我们用pandas实现这一功能。

1、调整列的顺序


>>> df = pd.read_excel(r'D:/myExcel/1.xlsx')
>>> df
  A B C D
0  bob 12 78 87
1 millor 15 92 21
>>> df.columns
Index(['A', 'B', 'C', 'D'], dtype='object')
# 这是最简单常用的一种方法,相当于指定列名让pandas
# 从df中获取
>>> df[['A', 'D', 'C', 'B']]
  A D C B
0  bob 87 78 12
1 millor 21 92 15
# 这也是可以的
>>> df[['A', 'A', 'A', 'A']]
  A  A  A  A
0  bob  bob  bob  bob
1 millor millor millor millor

2、添加某一列或者某几列

(1)直接添加


>>> df['E']=[1, 2]
>>> df
  A B C D E
0  bob 12 78 87 1
1 millor 15 92 21 2

(2)调用assign方法。该方法善于根据已有的列添加新的列,通过基本运算,或者调用函数


>>> df
  A B C D
0  bob 12 78 87
1 millor 15 92 21
# 其中E是列名,根据B列-C列的值得到
>>> df.assign(E=df['B'] - df['C'])
  A B C D E
0  bob 12 78 87 -66
1 millor 15 92 21 -77
# 添加两列也可以
>>> df.assign(E=df['B'] - df['C'], F=df['B'] * df['C'])
  A B C D E  F
0  bob 12 78 87 -66 936
1 millor 15 92 21 -77 1380

哈哈,以上就是pandas关于调整列的顺序以及新增列的用法

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

修改列名:

直接调用接口:


df.rename()

看一下接口中的定义:


 def rename(self, *args, **kwargs):
  """
  Alter axes labels.
  Function / dict values must be unique (1-to-1). Labels not contained in
  a dict / Series will be left as-is. Extra labels listed don't throw an
  error.
  See the :ref:`user guide <basics.rename>` for more.
  Parameters
  ----------
  mapper, index, columns : dict-like or function, optional
   dict-like or functions transfORMations to apply to
   that axis' values. Use either ``mapper`` and ``axis`` to
   specify the axis to target with ``mapper``, or ``index`` and
   ``columns``.
  axis : int or str, optional
   Axis to target with ``mapper``. Can be either the axis name
   ('index', 'columns') or number (0, 1). The default is 'index'.
  copy : boolean, default True
   Also copy underlying data
  inplace : boolean, default False
   Whether to return a new DataFrame. If True then value of copy is
   ignored.
  level : int or level name, default None
   In case of a MultiIndex, only rename labels in the specified
   level.
  Returns
  -------
  renamed : DataFrame
  See Also
  --------
  pandas.DataFrame.rename_axis
  Examples
  --------
  ``DataFrame.rename`` supports two calling conventions
  * ``(index=index_mapper, columns=columns_mapper, ...)``
  * ``(mapper, axis={'index', 'columns'}, ...)``
  We *highly* recommend using keyWord arguments to clarify your
  intent.
  >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
  >>> 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
 
  Using axis-style parameters
 
  >>> df.rename(str.lower, axis='columns')
   a b
  0 1 4
  1 2 5
  2 3 6
 
  >>> df.rename({1: 2, 2: 4}, axis='index')
   A B
  0 1 4
  2 2 5
  4 3 6
  """
  axes = validate_axis_style_args(self, args, kwargs, 'mapper', 'rename')
  kwargs.update(axes)
  # Pop these, since the values are in `kwargs` under different names
  kwargs.pop('axis', None)
  kwargs.pop('mapper', None)
  return super(DataFrame, self).rename(**kwargs)

注意:

一个*,输入可以是数组、元组,会把输入的数组或元组拆分成一个个元素。

两个*,输入必须是字典格式

示例:


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

调整列的顺序:

如:


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

直接修改列名:


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

就可以了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。如有错误或未考虑完全的地方,望不吝赐教。

--结束END--

本文标题: pandas调整列的顺序以及添加列的实现

本文链接: https://www.lsjlt.com/news/122457.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • pandas调整列的顺序以及添加列的实现
    在对excel的操作中,调整列的顺序以及添加一些列也是经常用到的,下面我们用pandas实现这一功能。 1、调整列的顺序 >>> df = pd.read_ex...
    99+
    2022-11-12
  • 使用pandas怎么调整列的顺序
    这篇文章给大家介绍使用pandas怎么调整列的顺序,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。调整列的顺序>>> df = pd.read_excel(r'D:...
    99+
    2023-06-14
  • pandas添加自增列的2种实现方案
    有时候我们需要添加一列自动增加数字的列,可以用下面两种方法: 第一种 >>> import pandas as pd >>> df = pd....
    99+
    2022-11-11
  • SQL Server 数据库调整表中列的顺序操作方法及遇到问题
    SQL Server 数据库中表一旦创建,我们不建议擅自调整列的顺序,特别是对应的应用系统已经上线,因为部分开发人员,不一定在代码中指明了列名。表是否可以调整列的顺序,其实可以自主设置,我们建议在安装后设置...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作