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 sqlobject(mysql)中文乱码解决方法
Nov 14 Python
使用Python下的XSLT API进行web开发的简单教程
Apr 15 Python
分享Python字符串关键点
Dec 13 Python
Python的Tornado框架实现异步非阻塞访问数据库的示例
Jun 30 Python
详解Python中的相对导入和绝对导入
Jan 06 Python
Python使用三种方法实现PCA算法
Dec 12 Python
Python判断中文字符串是否相等的实例
Jul 06 Python
python flask框架实现传数据到js的方法分析
Jun 11 Python
对django的User模型和四种扩展/重写方法小结
Aug 17 Python
Pytorch卷积层手动初始化权值的实例
Aug 17 Python
Python time库的时间时钟处理
May 02 Python
Python内置数据结构列表与元组示例详解
Aug 04 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
header中Content-Disposition的作用与使用方法
2012/06/13 PHP
PHP实现UTF-8文件BOM自动检测与移除实例
2014/11/05 PHP
PHP实现递归目录的5种方法
2016/10/27 PHP
PHP中16个高危函数整理
2019/09/19 PHP
浅析PHP反序列化中过滤函数使用不当导致的对象注入问题
2020/02/15 PHP
Prototype Number对象 学习
2009/07/19 Javascript
JS小游戏之仙剑翻牌源码详解
2014/09/25 Javascript
jquery实现textarea 高度自适应
2015/03/11 Javascript
jQuery+PHP+MySQL二级联动下拉菜单实例讲解
2015/10/27 Javascript
jQuery自定义动画函数实例详解(附demo源码)
2015/12/10 Javascript
js判断文本框输入的内容是否为数字
2015/12/23 Javascript
jQuery Dialog对话框事件用法实例分析
2016/05/10 Javascript
基于JS模仿windows文件按名称排序效果
2016/06/29 Javascript
关于vue.js弹窗组件的知识点总结
2016/09/11 Javascript
jQuery实现ajax的嵌套请求案例分析
2019/02/16 jQuery
vue表单中遍历表单操作按钮的显示隐藏示例
2019/10/30 Javascript
Vue移动端实现图片上传及超过1M压缩上传
2019/12/23 Javascript
Python 基础之字符串string详解及实例
2017/04/01 Python
Python 编码Basic Auth使用方法简单实例
2017/05/25 Python
Python实现字符串与数组相互转换功能示例
2017/09/22 Python
Python3实现将一维数组按标准长度分隔为二维数组
2019/11/29 Python
Python3 A*寻路算法实现方式
2019/12/24 Python
Python 存取npy格式数据实例
2020/07/01 Python
CSS3制作hover下划线动画
2017/03/27 HTML / CSS
COACH德国官方网站:纽约现代奢侈品牌,1941年
2018/06/09 全球购物
怎样声明子类
2013/07/02 面试题
优纳科技软件测试面试题
2012/05/15 面试题
应聘自荐书
2013/10/08 职场文书
大课间活动制度
2014/01/18 职场文书
关于圣诞节的广播稿
2014/01/26 职场文书
人事专员职责
2014/02/22 职场文书
《雨点儿》教学反思
2014/04/14 职场文书
市场营销工作计划书
2014/09/15 职场文书
党的群众路线调研报告
2014/11/03 职场文书
同乡会致辞
2015/07/30 职场文书
React更新渲染原理深入分析
2022/12/24 Javascript