Python Pandas 对列/行进行选择,增加,删除操作


Posted in Python onMay 17, 2020

一、列操作

1.1 选择列

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print (df ['one'])
# 选择其中一列进行显示,列长度为最长列的长度
# 除了 index 和 数据,还会显示 列表头名,和 数据 类型

运行结果:

a    1.0
b    2.0
c    3.0
d    NaN
Name: one, dtype: float64

1.2 增加列

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

# Adding a new column to an existing DataFrame object with column label by passing new series

print ("Adding a new column by passing as Series:")
df['three']=pd.Series([10,30,20],index=['a','c','b'])
print(df)
# 增加列后进行显示,其中 index 用于对应到该列 元素 位置(所以位置可以不由 列表 中的顺序进行指定)

print ("Adding a new column using the existing columns in DataFrame:")
df['four']=df['one']+df['two']+df['three']
print(df)
# 我们选定列后,直接可以对整个列的元素进行批量运算操作,这里 NaN 与其他元素相加后,还是 NaN

运行结果:

Adding a new column by passing as Series:
   one  two  three
a  1.0    1   10.0
b  2.0    2   20.0
c  3.0    3   30.0
d  NaN    4    NaN
Adding a new column using the existing columns in DataFrame:
   one  two  three  four
a  1.0    1   10.0  12.0
b  2.0    2   20.0  24.0
c  3.0    3   30.0  36.0
d  NaN    4    NaN   NaN

1.3 删除列(del 和 pop 函数)

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
  'three' : pd.Series([10,20,30], index=['a','b','c'])}

df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)

# 使用 del 函数
print ("Deleting the first column using DEL function:")
del(df['one'])
print(df)

# 使用 pop 函数
print ("Deleting another column using POP function:")
df_2=df.pop('two') # 将一列 pop 到新的 dataframe
print(df_2)
print(df)

运行结果:

Our dataframe is:
   one  two  three
a  1.0    1   10.0
b  2.0    2   20.0
c  3.0    3   30.0
d  NaN    4    NaN
Deleting the first column using DEL function:
   two  three
a    1   10.0
b    2   20.0
c    3   30.0
d    4    NaN
Deleting another column using POP function:
   three
a   10.0
b   20.0
c   30.0
d    NaN
POP column:
a    1
b    2
c    3
d    4
Name: two, dtype: int64

二、行操作

2.1 选择行

2.1.1 通过 label 选择行(loc 函数)

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df.loc['b']) # 显示这一行中,对应表头 下的 对应数据,同时显示 行 index 和 数据类型

运行结果:

one    2.0
two    2.0
Name: b, dtype: float64

2.1.2 通过序号选择行(iloc 函数)

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df.iloc[2]) # 序号 2 对应的是第 3 行的数据

运行结果:

one    3.0
two    3.0
Name: c, dtype: float64

2.1.3 通过序号选择行切片

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
  'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df[2:4]) # 这里选择第 3 到 第 4 行,与 Python 切片一致,不需要函数,直接切片即可

运行结果:

   one  two
c  3.0    3
d  NaN    4

2.2 增加行(append 函数)

# 通过 append 函数
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)
print(df) # 这里相当于把 第二个 dataframe 与第一个进行拼接,默认的 index 都是 0 1
print(df.loc[0]) # 这里有两行的 index 是 0

运行结果:

   a  b
0  1  2
1  3  4
0  5  6
1  7  8
   a  b
0  1  2
0  5  6

2.3 删除行(drop 函数)

# 通过 drop 函数
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)

df = df.drop(0) # 这里有两个行标签为 0,所以直接删除了 2 行
print(df)

运行结果:

   a  b
1  3  4
1  7  8

到此这篇关于Python Pandas 对列/行进行选择,增加,删除操作的文章就介绍到这了,更多相关Python Pandas行列选择增加删除内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
总结Python编程中三条常用的技巧
May 11 Python
详解Python网络爬虫功能的基本写法
Jan 28 Python
Python自定义函数实现求两个数最大公约数、最小公倍数示例
May 21 Python
python3 实现一行输入,空格隔开的示例
Nov 14 Python
pyqt5 comboBox获得下标、文本和事件选中函数的方法
Jun 14 Python
使用python的pandas为你的股票绘制趋势图
Jun 26 Python
python实现将文件夹内的每张图片批量分割成多张
Jul 22 Python
Python操作Jira库常用方法解析
Apr 10 Python
Python变量格式化输出实现原理解析
Aug 06 Python
Windows下pycharm安装第三方库失败(通用解决方案)
Sep 17 Python
如何用python实现一个HTTP连接池
Jan 14 Python
Python扫描端口的实现
Jan 25 Python
用python打开摄像头并把图像传回qq邮箱(Pyinstaller打包)
May 17 #Python
Python键鼠操作自动化库PyAutoGUI简介(小结)
May 17 #Python
python 实现PIL模块在图片画线写字
May 16 #Python
在python image 中实现安装中文字体
May 16 #Python
Python 判断时间是否在时间区间内的实例
May 16 #Python
解决Python Matplotlib绘图数据点位置错乱问题
May 16 #Python
Python填充任意颜色,不同算法时间差异分析说明
May 16 #Python
You might like
js下函数般调用正则的方法附代码
2008/06/22 PHP
PHP分页初探 一个最简单的PHP分页代码的简单实现
2016/06/21 PHP
php中分页及SqlHelper类用法实例
2017/01/12 PHP
Prototype1.5 rc2版指南最后一篇之Position
2007/01/10 Javascript
将json当数据库一样操作的javascript lib
2013/10/28 Javascript
js数组转json并在后台对其解析具体实现
2013/11/20 Javascript
基于jquery实现的图片在各种分辨率下未知的容器内上下左右居中
2014/05/11 Javascript
js解决select下拉选不中问题
2014/10/14 Javascript
js+css实现tab菜单切换效果的方法
2015/01/20 Javascript
JavaScript中使用Math.floor()方法对数字取整
2015/06/15 Javascript
举例讲解JavaScript中将数组元素转换为字符串的方法
2015/10/25 Javascript
jQuery实现为控件添加水印文字效果(附源码)
2015/12/02 Javascript
Javascript下拉刷新的简单实现
2017/02/14 Javascript
详解RequireJS按需加载样式文件
2017/04/12 Javascript
浅谈vue,angular,react数据双向绑定原理分析
2017/11/28 Javascript
深入浅出理解JavaScript闭包的功能与用法
2018/08/01 Javascript
利用jqgrid实现上移下移单元格功能
2018/11/07 Javascript
谈谈JavaScript中的垃圾回收机制
2020/09/17 Javascript
小程序点餐界面添加购物车左右摆动动画
2020/09/23 Javascript
[02:01]2018完美盛典-开场舞《双子星》
2018/12/16 DOTA
解析Python中的二进制位运算符
2015/05/13 Python
Python Socket使用实例
2017/12/18 Python
详解python中的线程
2018/02/10 Python
Python3 sys.argv[ ]用法详解
2019/10/24 Python
Django如何使用jwt获取用户信息
2020/04/21 Python
使用JS+CSS3技术:让你的名字动起来
2013/04/27 HTML / CSS
HTML5 Canvas中绘制矩形实例
2015/01/01 HTML / CSS
名词解释型面试题(主要是网络)
2013/12/27 面试题
司法局群众路线教育实践活动整改措施
2014/09/17 职场文书
导师对论文的学术评语
2015/01/04 职场文书
2015年物业管理工作总结
2015/04/23 职场文书
婚姻出轨保证书
2015/05/08 职场文书
排球赛新闻稿
2015/07/17 职场文书
班主任培训研修日志
2015/11/13 职场文书
2021年最新用于图像处理的Python库总结
2021/06/15 Python
CSS 实现Chrome标签栏的技巧
2021/08/04 HTML / CSS