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 encode和decode的妙用
Sep 02 Python
Python入门之三角函数tan()函数实例详解
Nov 08 Python
浅谈Python在pycharm中的调试(debug)
Nov 29 Python
python Selenium实现付费音乐批量下载的实现方法
Jan 24 Python
解决Pandas的DataFrame输出截断和省略的问题
Feb 08 Python
python实现桌面气泡提示功能
Jul 29 Python
关于python3中setup.py小概念解析
Aug 22 Python
Django User 模块之 AbstractUser 扩展详解
Mar 11 Python
python3:excel操作之读取数据并返回字典 + 写入的案例
Sep 01 Python
用python读取xlsx文件
Dec 17 Python
pytorch中[..., 0]的用法说明
May 20 Python
Python实现列表拼接和去重的三种方式
Jul 02 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 带逗号千位符数字的处理方法
2012/01/10 PHP
PHP 简易输出CSV表格文件的方法详解
2013/06/20 PHP
如何解决CI框架的Disallowed Key Characters错误提示
2013/07/05 PHP
php版小黄鸡simsimi聊天机器人接口分享
2014/01/26 PHP
CI框架中site_url()和base_url()的区别
2015/01/07 PHP
thinkphp中多表查询中防止数据重复的sql语句(必看)
2016/09/22 PHP
javascript之对系统的toFixed()方法的修正
2007/05/08 Javascript
网页加载时页面显示进度条加载完成之后显示网页内容
2012/12/23 Javascript
Nodejs为什么选择javascript为载体语言
2015/01/13 NodeJs
JavaScript中的this到底是什么(一)
2015/12/09 Javascript
JavaScript获取对象在页面中位置坐标的方法
2016/02/03 Javascript
js仿QQ邮箱收件人选择与搜索功能
2017/02/10 Javascript
Javascript调试之console对象——你不知道的一些小技巧
2017/07/10 Javascript
Vue.js 的移动端组件库mint-ui实现无限滚动加载更多的方法
2017/12/23 Javascript
vue-router的使用方法及含参数的配置方法
2018/11/13 Javascript
微信小程序如何实现全局重新加载
2019/06/05 Javascript
vscode+gulp轻松开发小程序的完整步骤
2020/10/18 Javascript
[00:32]10月24、25日 辉夜杯外卡赛附加赛开赛!
2015/10/23 DOTA
python根据距离和时长计算配速示例
2014/02/16 Python
Python实现定时任务
2017/02/08 Python
一个基于flask的web应用诞生 用户注册功能开发(5)
2017/04/11 Python
小白入门篇使用Python搭建点击率预估模型
2018/10/12 Python
python utc datetime转换为时间戳的方法
2019/01/15 Python
PyQt5重写QComboBox的鼠标点击事件方法
2019/06/25 Python
Python实现企业微信机器人每天定时发消息实例
2020/02/25 Python
jupyter notebook 多行输出实例
2020/04/09 Python
将一个数的从第5位开始的7个数取出,其余位置0
2016/05/26 面试题
汇科协同Java笔试题
2012/03/31 面试题
简历中自我评价分享
2013/10/09 职场文书
应届毕业生自荐信例文
2014/02/26 职场文书
王力宏牛津大学演讲稿
2014/05/22 职场文书
2015年秋季学校开学标语
2015/07/16 职场文书
治庸问责工作总结
2015/08/11 职场文书
eclipse创建项目没有dynamic web的解决方法
2021/06/24 Java/Android
详细谈谈JavaScript中循环之间的差异
2021/08/23 Javascript
Java实现扫雷游戏详细代码讲解
2022/05/25 Java/Android