本篇文章给大家带来的内容是关于Python中Pandas读取修改excel操作攻略(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
环境:python 3.6.8
以某米赛尔号举个例子吧:
>>> pd.read_excel('1.xlsx', sheet_name='Sheet2') 名字 等级 属性1 属性2 天赋 0 四九幻曦 100 自然 None 21 1 圣甲狂战 100 战斗 None 0 2 时空界皇 100 光 次元 27
我们在这里使用了pd.read_excel()
函数来读取excel,来看一下read_excel()
这个方法的API,这里只截选一部分经常使用的参数:
pd.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None)
io
:很明显, 是excel文件的路径+名字字符串
(有中文的话python2
的老铁需要使用decode()
来解码成unicode字符串
)
例如:
>>> pd.read_excel('例子'.decode('utf-8))
sheet_name
:返回指定的sheet
如果将sheet_name
指定为None
,则返回全表
如果需要返回多个表, 可以将sheet_name
指定为一个列表, 例如['sheet1', 'sheet2']可以根据
sheet
的名字字符串或索引来值指定所要选取的sheet
>>> # 如: >>> pd.read_excel('1.xlsx', sheet_name=0) >>> pd.read_excel('1.xlsx', sheet_name='Sheet1') >>> # 返回的是相同的 DataFrame
header:指定数据表的表头,默认值为0, 即将第一行作为表头
usecols:读取指定的列, 也可以通过名字或索引值
>>> # 如: >>> pd.read_excel('1.xlsx', sheet_name=1, usecols=['等级', '属性1']) >>> pd.read_excel('1.xlsx', sheet_name=1, usecols=[1,2]) >>> # 返回的是相同的 DataFrame
直到某一天泰格尔升了一级, 可以这样改一下, 当然用.iloc
或.loc
对象都可以
>>> # 读取文件 >>> data = pd.read_excel("1.xlsx", sheet_name="Sheet1") >>> # 找到 等级 这一列,再在这一列中进行比较 >>> data['等级'][data['名字'] == '泰格尔'] += 1 >>> print(data)
LOOK!他升级了!!
>>> data 名字 等级 属性1 属性2 天赋 0 艾欧里娅 100 自然 冰 29 1 泰格尔 81 电 战斗 16 2 布鲁克克 100 水 None 28
现在我们将它保存
data.to_excel('1.xlsx', sheet_name='Sheet1', index=False, header=True)
index:默认为True
, 是否加行索引, 直接上图吧!![]()
左为False
, 右为True
header:默认为True
, 是否加列标, 上图吧!左为
False
, 右为True
而io, sheet_name
参数用法同函数pd.read_excel()
如果我们多捕捉几只或者多加几种属性怎么办呢?这里给出参考:
新增列数据:
data['列名称'] = [值1, 值2, ......]
>>> data['特性'] = ['瞬杀', 'None', '炎火'] >>> data 名字 等级 属性1 属性2 天赋 特性 0 艾欧里娅 100 自然 冰 29 瞬杀 1 泰格尔 80 电 战斗 16 None 2 布鲁克克 100 水 None 28 炎火
新增行数据,这里行的num为excel中自动给行加的id数值
data.loc[行的num] = [值1, 值2, ...], (注意与.iloc
的区别)
>>> data.loc[3] = ['小火猴', 1, '火', 'None', 31, 'None'] >>> data 名字 等级 属性1 属性2 天赋 特性 0 艾欧里娅 100 自然 冰 29 瞬杀 1 泰格尔 80 电 战斗 16 None 2 布鲁克克 100 水 None 28 炎火 3 小火猴 1 火 None 31 None
说完了增加一行或一列,那怎样删除一行或一列呢?可以使用.drop()
函数
>>> # 删除列, 需要指定axis为1,当删除行时,axis为0 >>> data = data.drop('属性1', axis=1) # 删除`属性1`列 >>> data 名字 等级 属性2 天赋 特性 0 艾欧里娅 100 冰 29 瞬杀 1 泰格尔 80 战斗 16 None 2 布鲁克克 100 None 28 炎火 3 小火猴 1 None 31 None >>> # 删除第3,4行,这里下表以0开始,并且标题行不算在类, axis用法同上 >>> data = data.drop([2, 3], axis=0) >>> data 名字 等级 属性2 天赋 特性 0 艾欧里娅 100 冰 29 瞬杀 1 泰格尔 80 战斗 16 None >>> # 保存 >>> data.to_excel('2.xlsx', sheet_name='Sheet1', index=False, header=True)
以上是Python中Pandas读取修改excel操作攻略(代码示例)的详细内容。更多信息请关注PHP中文网其他相关文章!

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。