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基础教程之序列详解
Aug 29 Python
python中文分词教程之前向最大正向匹配算法详解
Nov 02 Python
python调用系统ffmpeg实现视频截图、http发送
Mar 06 Python
python处理数据,存进hive表的方法
Jul 04 Python
python3爬虫学习之数据存储txt的案例详解
Apr 24 Python
Python Flask 搭建微信小程序后台详解
May 06 Python
django框架防止XSS注入的方法分析
Jun 21 Python
对python中的控制条件、循环和跳出详解
Jun 24 Python
Django项目主urls导入应用中views的红线问题解决
Aug 10 Python
详细整理python 字符串(str)与列表(list)以及数组(array)之间的转换方法
Aug 30 Python
python实现大学人员管理系统
Oct 25 Python
音频处理 windows10下python三方库librosa安装教程
Jun 20 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
Zend Studio去除编辑器的语法警告设置方法
2012/10/24 PHP
phpexcel导出excel的颜色和网页中的颜色显示不一致
2012/12/11 PHP
修改php.ini不生效问题解决方法(上传大于8M的文件)
2013/06/14 PHP
php中mt_rand()随机数函数用法
2014/11/24 PHP
php获取指定范围内最接近数的方法
2015/06/02 PHP
php之可变变量的实例详解
2017/09/12 PHP
php查询内存信息操作示例
2019/05/09 PHP
js有关元素内容操作小结
2011/12/20 Javascript
JS打开图片另存为对话框实现代码
2012/12/26 Javascript
JQuery中上下文选择器实现方法
2015/05/18 Javascript
JavaScript前端开发之实现二进制读写操作
2015/11/04 Javascript
js弹出对话框方式小结
2015/11/17 Javascript
JavaScript实现事件的中断传播和行为阻止方法示例
2017/01/20 Javascript
Javascript实现信息滚动效果
2017/05/18 Javascript
bootstrap动态添加面包屑(breadcrumb)及其响应事件的方法
2017/05/25 Javascript
JavaScript hasOwnProperty() 函数实例详解
2017/08/04 Javascript
jquery中ajax请求后台数据成功后既不执行success也不执行error的完美解决方法
2017/12/24 jQuery
JS加密插件CryptoJS实现的DES加密示例
2018/08/16 Javascript
解决Vue.js父组件$on无法监听子组件$emit触发事件的问题
2018/09/12 Javascript
JavaScript使用localStorage存储数据
2019/09/25 Javascript
[03:09]DOTA2亚洲邀请赛 LGD战队出场宣传片
2015/02/07 DOTA
[01:06:39]DOTA2上海特级锦标赛主赛事日 - 1 胜者组第一轮#1Liquid VS Alliance第三局
2016/03/02 DOTA
windows系统中python使用rar命令压缩多个文件夹示例
2014/05/06 Python
初学Python函数的笔记整理
2015/04/07 Python
web.py 十分钟创建简易博客实现代码
2016/04/22 Python
浅谈python3发送post请求参数为空的情况
2018/12/28 Python
Python响应对象text属性乱码解决方案
2020/03/31 Python
python实现图片转字符画
2021/02/19 Python
CSS3实现苹果手机解锁的字体闪亮效果示例
2021/01/05 HTML / CSS
竞聘副主任科员演讲稿
2014/01/11 职场文书
物流专业大学生职业生涯规划书范文
2014/01/15 职场文书
三月学雷锋月活动总结
2014/04/28 职场文书
教代会开幕词
2015/01/28 职场文书
举起手来观后感
2015/06/09 职场文书
python3 实现mysql数据库连接池的示例代码
2021/04/17 Python
关于CSS浮动与取消浮动的问题
2021/06/28 HTML / CSS