Python Pandas 对列/行进行选择,增加,删除操作
作者:AItrust 发布时间:2022-10-29 03:55:52
一、列操作
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
来源:https://blog.csdn.net/qq_42067550/article/details/106163649


猜你喜欢
- CREATE TABLE tb(standards varchar(50), amount varchar(50), variation v
- 本文实例为大家分享了Python端口扫描的实现代码,供大家参考,具体内容如下获取本机的IP和端口号:import socket def ge
- win7 +Navicat Lite 9+ VMware7在VMware中安装openSUSE11.x mysql5 Navicat Lit
- vue中最常见的属v-model这个数据双向绑定了,很好奇它是如何实现的呢?尝试着用原生的JS去实现一下。首先大致学习了解下Object.d
- 现在有一个员工字典,类似这样的结构staff_dic = {"name":"灭霸", "a
- 本文主要介绍Python3.6及TensorFlow的安装和配置流程。一、Python官网下载自己电脑和系统对应的Python安装包。网址:
- 在Web自动化测试的过程中,经常会被登录的验证码给卡住,不知道如何去通过验证码的验证。一般的情况下遇到验证码我们可以都可以找开发去帮忙解决,
- 介绍init 方法通常用在初始化一个类实例时候,但其实它不是实例化一个类的时候第一个被调用 的方法。当使用 Student(id, name
- 一、心知天气API密钥获取首先,访问https://www.seniverse.com,进行登录或者注册操作,然后在控制台上创建一个免费版的
- Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义。一、函数式装饰器:装饰器本身是一个函数。1.装饰函数:被装饰对象
- 一、Python解释器 安装Windows平台下载地址 https://www.python.org/ftp/python/3.9.5/py
- 实现制作抽奖程序,需要认知到我们可以看到一般抽奖程序界面上是有很多按钮的,比如中奖区域,按键开始区域等等,所以我们先要设置界面,然后把这些按
- Web Forms 2.0 是一个很有意思的东东,是 HTML 5 的组成部分。它的目标是提升表单的使用性 (usability),基本上就
- 今天服务器重新安装mssql2005版本,为了安全让mssql2005运行在独立用户下,权限也没错误,但竟然出现了以下错误; -------
- 1.生成器# 一边循环一边计算的机制,称为生成器:generator;# 创建generator方法:# 1.把一个列表生成式的[]改成()
- 本文实例为大家分享了python实现多张图片垂直合并的具体代码,供大家参考,具体内容如下# coding: utf-8 # image_me
- 文本的排版依据语言的不同会有一些格式上的要求,比如简体中文中类似逗号、分号等标点符号不会出现在一行的开头,对于英文来讲就是一个完整单词不会在
- MySQL数据库配置技巧用root用户启动远程服务一直是安全大忌,因为如果服务程序出现问题,远程攻击者极有可能获得主机的完全控制权。MySQ
- 一般TensorFlow中扩展维度可以使用tf.expand_dims()。近来发现另一种可以直接运用取数据操作符[]就能扩展维度的方法。用
- 本文实例讲述了Flask框架学习笔记之模板操作。分享给大家供大家参考,具体如下:flask的模板引擎是Jinja2。引入模板的好处是增加程序