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
猜你喜欢
- 拿到需求之后,发现这两个网站的MYSQL数据库都不能远程访问(安全第一吧)。于是想起了 平时使用的CSV文件批量录入数据。于是 尝试使用CS
- 1 输出大写字母、小写字母、大小写字母、数字、大小写字母和数字1.1输出小写:找到小写a(97)到z(122)的的ASCII码,然后转义为字
- 如果要写一个程序,让x1为1,x2为2,然后直到x100为100,你会怎么做?在C这种静态语言里,变量名这个标识符实际上会被编译器直接翻译成
- Javascript函数类型判断完美解决方案在判断函数类型时,我们通常使用typeof方法,一般情况下,它会得到我们所预想的效果。但是,有一
- 详解Golang 与python中的字符串反转在go中,需要用rune来处理,因为涉及到中文或者一些字符ASCII编码大于255的。func
- 整理字符串输入整理用户输入的问题在编程过程中极为常见。通常情况下,将字符转换为小写或大写就够了,有时你可以使用正则表达式模块「Regex」完
- 在Python中,最基本的数据结构为序列。Python中包含6种内建序列:字符串、列表、元组、Unicode字符串、buffer对象、xra
- 生成器是 Python 的基本组成部分。生成器是一个至少有一个“yield”表达式的函数。它们是可以
- 本文实例为大家分享了wxPython分隔窗口的具体代码,供大家参考,具体内容如下1、分割窗口分隔窗口(wx.SplitterWindow)就
- 一 在写之前 最好指定python的路径:#!/usr/bin/pythonpython 在linux中需要添加编码方式:以免出现中文乱码#
- 我就废话不多说了,直接上代码吧!import cv2import osdef save_img(): video_path =
- 作用collate_fn:即用于collate的function,用于整理数据的函数。说到整理数据,你当然要会用数据,即会用数据制作工具to
- 如下所示:在def __init__(self):函数里添加self.setFixedSize(self.width(), self.hei
- 如何搭建完整的网站架构并设计出一个出色的网站?关于这个问题,我们很难提出一个绝对权威和正确的设计思路,但任何网站的设计都需要遵循一个循序渐进
- 我就废话不多说了,直接上代码吧!import turtlet=turtle.Turtle()turtle.Turtle().screen.d
- 前些天有位网友建议我在博客中添加RSS订阅功能,觉得挺好,所以自己抽空看了一下如何在Django中添加RSS功能,发现使用Django中的s
- 严格控制Session可以将不需要Session的内容(比如帮助画面,访问者区域,等等)移动到关闭Session的独立ASP应用程序中。在基
- 最近在研究网页的切片算法,很可能很多人不知道什么是切片算法,其实这是一种面向搜索引擎的网页分块、切片的原理,目前随着工作的深入,逐渐碰到了各
- 用Python随机生成学生姓名,三科成绩和班级数据,再插入到PostgreSQL中。模块用psycopg2 randomimport ran
- Python continue语句返回while循环的开始。Continue语句拒绝在该循环的当前迭代中的其余语句执行并移动控制