pandas怎样调整列的顺序?添加列的操作是什么?
Admin 2021-05-22 群英技术资讯 2175 次浏览
Pandas 是 Python 语言的一个扩展程序库,能用来数据分析。而且pandas还提供了大量能帮助我们快速便捷地处理数据的函数和方法。我们有时候需要对excel表的列做操作,使用pandas就是能实现我们想要的功能。下面我们就一起来看看使用pandas如何调整列的顺序。
在对excel的操作中,调整列的顺序以及添加一些列也是经常用到的,下面我们用pandas实现这一功能。
>>> 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
(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
就可以了。
以上就是关于pandas实现调整列的顺序的介绍,此外还介绍了添加了列的操作,感谢的朋友可以参考学习,希望对大家有帮助。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
uuid通过Python标准库的uuid模块生成通用唯一ID(或“UUID”)的一种快速简便的方法,下面这篇文章主要给大家介绍了关于Python标准库uuid模块(生成唯一标识) 的相关资料,需要的朋友可以参考下
整个Spark 框架模块包含:Spark Core、 Spark SQL、 Spark Streaming、 Spark GraphX、 Spark MLlib,而后四项的能力都是建立在核心引擎之上。Spark Core:Spark的核心,Spark核心功能均由Spark Core模块提供...
这篇文章主要为大家介绍了R语言条形图及分布密度图代码总结,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
property是一个类,可以把一个方法当做属性进行使用,这样做可以简化代码使用。实际上就是装饰类中属性的gettersetter方法,使得属性可以通过对象.属性的方式获取或设置 使用property的两种方式装饰器方式类属性方式2.装饰器方式@property修饰获取的方法getter,方法名必须和属性名一样@age.setter修饰设置值的方法sett
python中实现字符串分割的方法有哪些?split() 方法可以实现将一个字符串按照指定的分隔符切分成多个子串,通过该分割操作后,会返回一个列表。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008