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爬虫之爬虫框架Scrapy安装配置
Nov 06 Python
Python进阶学习之特殊方法实例详析
Dec 01 Python
机器学习的框架偏向于Python的13个原因
Dec 07 Python
Python编程实现线性回归和批量梯度下降法代码实例
Jan 04 Python
python 获取当天每个准点时间戳的实例
May 22 Python
kaggle+mnist实现手写字体识别
Jul 26 Python
解决python字典对值(值为列表)赋值出现重复的问题
Jan 20 Python
python中的&&及||的实现示例
Aug 07 Python
使用python3批量下载rbsp数据的示例代码
Dec 20 Python
基于FME使用Python过程图解
May 13 Python
python+selenium爬取微博热搜存入Mysql的实现方法
Jan 27 Python
Python实现视频自动打码的示例代码
Apr 08 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 3行代码的分页算法(求起始页和结束页)
2009/10/21 PHP
关于php内存不够用的快速解决方法
2013/10/26 PHP
php面向对象中static静态属性与方法的内存位置分析
2015/02/08 PHP
PHP crc32()函数讲解
2019/02/14 PHP
PHP getID3类的使用方法学习笔记【附getID3源码下载】
2019/10/18 PHP
javascript英文日期(有时间)选择器
2007/05/02 Javascript
Add a Picture to a Microsoft Word Document
2007/06/15 Javascript
jQuery 复合选择器应用的几个例子
2014/09/11 Javascript
js实现温度计时间样式代码分享
2015/08/21 Javascript
JavaScript优化专题之Loading and Execution加载和运行
2016/01/20 Javascript
jquery实现点击弹出可放大居中及关闭的对话框(附demo源码下载)
2016/05/10 Javascript
简单三步实现报表页面集成天气
2016/12/15 Javascript
Angular4编程之表单响应功能示例
2017/12/13 Javascript
详解vue-cli快速构建vue应用并实现webpack打包
2017/12/13 Javascript
浅谈Vue页面级缓存解决方案feb-alive(上)
2019/04/14 Javascript
layer.alert回调函数执行关闭弹窗的实例
2019/09/11 Javascript
[01:14:10]2014 DOTA2国际邀请赛中国区预选赛 SPD-GAMING VS Orenda
2014/05/22 DOTA
[01:51]2014DOTA2西雅图邀请赛 MVP 外卡赛black场间采访
2014/07/09 DOTA
python3.3教程之模拟百度登陆代码分享
2014/01/16 Python
Python datetime时间格式化去掉前导0
2014/07/31 Python
python根据开头和结尾字符串获取中间字符串的方法
2015/03/26 Python
Python中几种导入模块的方式总结
2017/04/27 Python
Django框架使用富文本编辑器Uedit的方法分析
2018/07/31 Python
Python实现的字典排序操作示例【按键名key与键值value排序】
2018/12/21 Python
Python 虚拟空间的使用代码详解
2019/06/10 Python
python实现树的深度优先遍历与广度优先遍历详解
2019/10/26 Python
Python如何在DataFrame增加数值
2020/02/14 Python
python统计文章中单词出现次数实例
2020/02/27 Python
浅谈ROC曲线的最佳阈值如何选取
2020/02/28 Python
如何在VSCode下使用Jupyter的教程详解
2020/07/13 Python
python中如何设置代码自动提示
2020/07/15 Python
安全生产实施方案
2014/02/23 职场文书
求职自荐信怎么写
2014/03/06 职场文书
教你怎么用Python selenium操作浏览器对象的基础API
2021/06/23 Python
Python爬虫入门案例之爬取二手房源数据
2021/10/16 Python
如何利用python创作字符画
2022/06/25 Python