【Python】excel常用函数操作Python实现,办公入门首选
off999 2024-11-05 10:55 25 浏览 0 评论
常见的Excel函数,在Python中的如何实现:
- VLOOKUP: 可以使用merge或map函数来实现类似的功能。
- IF: 可以使用numpy库的where函数来实现类似的功能。
- SUMIF: 可以使用pandas的query函数来筛选数据,然后使用sum函数来计算总和。
- COUNTIF: 类似于SUMIF,可以使用query函数来筛选数据,然后使用count函数来计算数量。
- AVERAGEIF: 类似于SUMIF和COUNTIF,可以使用query函数来筛选数据,然后使用mean函数来计算平均值。
- INDEX & MATCH: 可以使用.loc或.iloc函数来实现类似的功能。
- RANK: 可以使用rank函数来实现类似的功能。
- CONCATENATE: 可以使用+运算符或str.cat函数来实现类似的功能。
- TEXT: 可以使用str.format函数来实现类似的功能。
- SUBSTITUTE: 可以使用str.replace函数来实现类似的功能。
代码如下:
1.VLOOKUP:可以使用merge或map函数来实现类似的功能。
import pandas as pd
# Let's assume we have two dataframes: df1 and df2
df1 = pd.DataFrame({
'A': ['foo', 'bar', 'baz', 'qux'],
'B': ['one', 'one', 'two', 'three'],
'C': ['x', 'y', 'z', 'w'],
'D': [1, 2, 3, 4]
})
df2 = pd.DataFrame({
'B': ['one', 'two', 'three'],
'E': ['apple', 'banana', 'cherry']
})
# Now we want to add column E from df2 to df1 based on the values in column B
df3 = pd.merge(df1, df2, on='B', how='left')
print(df3)
输出结果:
- IF:Python中的numpy库提供了一个where函数,可以用来实现类似的功能。例如:
import numpy as np
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8]
})
# We want to create a new column C that contains the value from column A if the value in column B is greater than 6, otherwise it contains the value from column B
df['C'] = np.where(df['B'] > 6, df['A'], df['B'])
print(df)
- SUMIF:可以使用pandas的query函数来筛选数据,然后使用sum函数来计算总和。例如:
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': ['yes', 'no', 'yes', 'no']
})
# We want to sum the values in column A where the corresponding value in column B is 'yes'
sum_if = df.query('B == "yes"')['A'].sum()
print(sum_if)
- COUNTIF:类似于SUMIF,可以使用query函数来筛选数据,然后使用count函数来计算数量。例如:
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': ['yes', 'no', 'yes', 'no']
})
# We want to count the number of values in column A where the corresponding value in column B is 'yes'
count_if = df.query('B == "yes"')['A'].count()
print(count_if)
- AVERAGEIF:类似于SUMIF和COUNTIF,可以使用query函数来筛选数据,然后使用mean函数来计算平均值。例如:
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': ['yes', 'no', 'yes', 'no']
})
# We want to calculate the average of values in column A where the corresponding value in column B is 'yes'
average_if = df.query('B == "yes"')['A'].mean()
print(average_if)
- INDEX & MATCH:可以使用.loc或.iloc函数来实现类似的功能。例如:
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': ['a', 'b', 'c', 'd']
})
# We want to get the value in column A where the corresponding value in column B is 'c'
value = df.loc[df['B'] == 'c', 'A'].iloc[0]
print(value)
- RANK:可以使用rank函数来实现类似的功能。例如:
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [4, 3, 2, 1]
})
# We want to get the rank of values in column B
df['C'] = df['B'].rank()
print(df)
- CONCATENATE:可以使用+运算符或str.cat函数来实现类似的功能。例如:
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': ['Hello', ' ', 'World'],
'B': ['!', '', '']
})
# We want to concatenate the values in column A and column B
df['C'] = df['A'] + df['B']
# or
df['C'] = df['A'].str.cat(df['B'])
print(df)
- TEXT:可以使用str.format函数来实现类似的功能。例如:
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [0.1, 0.2, 0.3, 0.4]
})
# We want to format the values in column B as text with two decimal places
df['C'] = df['B'].apply(lambda x: '{:.2f}'.format(x))
print(df)
- SUBSTITUTE:可以使用str.replace函数来实现类似的功能。例如:
# Let's assume we have a dataframe df
df = pd.DataFrame({
'A': ['Hello World', 'Goodbye World']
})
# We want to replace 'World' with 'Python' in column A
df['B'] = df['A'].str.replace('World', 'Python')
print(df)
相关推荐
- 第九章:Python文件操作与输入输出
-
9.1文件的基本操作9.1.1打开文件理论知识:在Python中,使用open()函数来打开文件。open()函数接受两个主要参数:文件名和打开模式。打开模式决定了文件如何被使用,常见的模式有:&...
- Python的文件处理
-
一、文件处理的流程1.打开文件,得到文件句柄并赋值给一个变量2.通过句柄对文件进行操作3.关闭文件示例:d=open('abc')data1=d.read()pri...
- Python处理文本的25个经典操作
-
Python处理文本的优势主要体现在其简洁性、功能强大和灵活性。具体来说,Python提供了丰富的库和工具,使得对文件的读写、处理变得轻而易举。简洁的文件操作接口Python通过内置的open()函数...
- Python学不会来打我(84)python复制文件操作总结
-
上一篇文章我们分享了python读写文件的操作,主要用到了open()、read()、write()等方法。这一次是在文件读写的基础之上,我们分享文件的复制。#python##python自学##...
- python 文件操作
-
1.检查目录/文件使用exists()方法来检查是否存在特定路径。如果存在,返回True;如果不存在,则返回False。此功能在os和pathlib模块中均可用,各自的用法如下。#os模块中e...
- 《文件操作(读写文件)》
-
一、文件操作基础1.open()函数核心语法file=open("filename.txt",mode="r",encoding="utf-8"...
- 栋察宇宙(二十一):Python 文件操作全解析
-
分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“Python文件操作全解析”欢迎您的访问!Sharethefun,spreadthe...
- 值得学习练手的70个Python项目(附代码),太实用了
-
Python丰富的开发生态是它的一大优势,各种第三方库、框架和代码,都是前人造好的“轮子”,能够完成很多操作,让你的开发事半功倍。下面就给大家介绍70个通过Python构建的项目,以此来学习Pytho...
- python图形化编程:猜数字的游戏
-
importrandomnum=random.randint(1,500)running=Truetimes=0##总的次数fromtkinterimport*##导入所有tki...
- 一文讲清Python Flask的Web编程知识
-
刚入坑Python做Web开发的新手,还在被配置臃肿、启动繁琐折磨?Flask这轻量级框架最近又火出圈,凭5行代码启动Web服务的极致简洁,让90后程序员小张直呼真香——毕竟他刚用这招把部署时间从半小...
- 用python 编写一个hello,world
-
第一种:交互式运行一个hello,world程序:这是写python的第一步,也是学习各类语言的第一步,就是用这种语言写一个hello,world程序.第一步,打开命令行窗口,输入python,第二步...
- python编程:如何使用python代码绘制出哪些常见的机器学习图像?
-
专栏推荐绘图的变量单变量查看单变量最方便的无疑是displot()函数,默认绘制一个直方图,并你核密度估计(KDE)sns.set(color_codes=True)np.random.seed(su...
- 如何编写快速且更惯用的 Python 代码
-
Python因其可读性而受到称赞。这使它成为一种很好的第一语言,也是脚本和原型设计的流行选择。在这篇文章中,我们将研究一些可以使您的Python代码更具可读性和惯用性的技术。我不仅仅是pyt...
- Python函数式编程的详细分析(代码示例)
-
本篇文章给大家带来的内容是关于Python函数式编程的详细分析(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。FunctionalProgramming,函数式编程。Py...
- 编程小白学做题:Python 的经典编程题及详解,附代码和注释(七)
-
适合Python3+的6道编程练习题(附详解)1.检查字符串是否以指定子串开头题目描述:判断字符串是否以给定子串开头(如"helloworld"以"hello&...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (77)
- python封装 (57)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)