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中的五种异常处理机制介绍
Sep 02 Python
python 3.6 +pyMysql 操作mysql数据库(实例讲解)
Dec 20 Python
Python实现爬虫设置代理IP和伪装成浏览器的方法分享
May 07 Python
基于python log取对数详解
Jun 08 Python
详解Python中的分组函数groupby和itertools)
Jul 11 Python
python 实现识别图片上的数字
Jul 30 Python
Python字典中的值为列表或字典的构造实例
Dec 16 Python
在Python中通过threshold创建mask方式
Feb 19 Python
Python如何批量获取文件夹的大小并保存
Mar 31 Python
Python3-异步进程回调函数(callback())介绍
May 02 Python
MATLAB 如何求取离散点的曲率最大值
Apr 16 Python
用PYTHON去计算88键钢琴的琴键频率和音高
Apr 10 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
PHP 验证码的实现代码
2011/07/17 PHP
PHP实现简单的模板引擎功能示例
2017/09/02 PHP
PHP simplexml_load_file()函数讲解
2019/02/03 PHP
PHP中通过getopt解析GNU C风格命令行选项
2019/11/18 PHP
document.designMode的功能与使用方法介绍
2007/11/22 Javascript
将函数的实际参数转换成数组的方法
2010/01/25 Javascript
让AJAX不依赖后端接口实现方案
2012/12/03 Javascript
javascript返回顶部效果(自写代码)
2013/01/06 Javascript
js数值和和字符串进行转换时可以对不同进制进行操作
2014/03/05 Javascript
开发中可能会用到的jQuery小技巧
2014/03/07 Javascript
再谈Jquery Ajax方法传递到action(补充)
2014/05/12 Javascript
推荐10个2014年最佳的jQuery视频插件
2014/11/12 Javascript
jQuery事件绑定和委托实例
2014/11/25 Javascript
javascript+html5实现仿flash滚动播放图片的方法
2015/04/27 Javascript
js闭包所用的场合以及优缺点分析
2015/06/22 Javascript
输入法的回车与消息发送快捷键回车的冲突解决方法
2016/08/09 Javascript
Angular 2父子组件之间共享服务通信的实现
2017/07/04 Javascript
vue2.0+koa2+mongodb实现注册登录
2018/04/10 Javascript
Vue源码探究之虚拟节点的实现
2019/04/17 Javascript
nodejs环境使用Typeorm连接查询Oracle数据
2019/12/05 NodeJs
vue 出现data-v-xxx的原因及解决
2020/08/04 Javascript
js实现滚动条自动滚动
2020/12/13 Javascript
[41:56]Spirit vs Liquid Supermajor小组赛A组 BO3 第一场 6.2
2018/06/03 DOTA
[51:53]完美世界DOTA2联赛决赛日 Inki vs LBZS 第二场 11.08
2020/11/10 DOTA
Saltstack快速入门简单汇总
2016/03/01 Python
python根据unicode判断语言类型实例代码
2018/01/17 Python
Linux下python与C++使用dlib实现人脸检测
2018/06/29 Python
Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】
2018/07/12 Python
Django 实现将图片转为Base64,然后使用json传输
2020/03/27 Python
网页布局中CSS样式无效的十个重要原因详解
2017/08/10 HTML / CSS
意大利中国电子产品购物网站:Geekmall.com
2019/09/30 全球购物
澳大利亚顶级美发和美容贸易超市:glamaCo
2020/01/19 全球购物
装饰活动策划方案
2014/02/11 职场文书
高中生操行评语大全
2014/04/25 职场文书
作风年建设汇报材料
2014/08/14 职场文书
pd.drop_duplicates删除重复行的方法实现
2022/06/16 Python