


Pandas read and modify excel operation strategy in Python (code example)
The content of this article is about the Pandas read and modify excel operation guide (code example) in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Environment: python 3.6.8
Let’s take a certain Miser number as an example:
>>> pd.read_excel('1.xlsx', sheet_name='Sheet2') 名字 等级 属性1 属性2 天赋 0 四九幻曦 100 自然 None 21 1 圣甲狂战 100 战斗 None 0 2 时空界皇 100 光 次元 27
We use the pd.read_excel()
function here to read excel. Let’s take a look at the API of read_excel()
. Here we only intercept Some commonly used parameters:
pd.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None)
io
: Obviously, it is the path name string of the excel file
(if there is Chinese, it is python2
Laotie needs to use decode()
to decode into unicode string
)
For example:
>>> pd.read_excel('例子'.decode('utf-8))
header: Specify the header of the data table, the default value is 0, that is, the first row will be used as the headerusecols: Read the specified column, you can also use the name or index valuesheet_name
: Return the specified sheet
Ifsheet_name
is specified asNone
, the entire sheet will be returned
If multiple sheets need to be returned,sheet_name## can be returned #Specify as a list, for example ['sheet1', 'sheet2']
You can specify the selectedsheet
based on the name string or index of
sheet
>>> # 如: >>> pd.read_excel('1.xlsx', sheet_name=0) >>> pd.read_excel('1.xlsx', sheet_name='Sheet1') >>> # 返回的是相同的 DataFrameCopy after login
Until one day Tiger reaches a level, you can change it like this. Of course, you can use>>> # 如: >>> pd.read_excel('1.xlsx', sheet_name=1, usecols=['等级', '属性1']) >>> pd.read_excel('1.xlsx', sheet_name=1, usecols=[1,2]) >>> # 返回的是相同的 DataFrameCopy after login
.iloc or
.loc objects
>>> # 读取文件 >>> data = pd.read_excel("1.xlsx", sheet_name="Sheet1") >>> # 找到 等级 这一列,再在这一列中进行比较 >>> data['等级'][data['名字'] == '泰格尔'] += 1 >>> print(data)
>>> 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)
TrueLeft is, whether to add row index, just go to the picture above!
![]()
Falseheader: Default is, right is
True
Trueand, whether Add a column mark and picture it!
The left one isFalse
, the right one is
True
io, sheet_nameWhat if we capture a few more animals or add a few more attributes? Reference is given here: New column data:parameter usage is the same as function
pd. read_excel()
data['column name'] = [value 1, value 2, ...]New Row data, the num of the row here is the id value automatically added to the row in excel>>> data['特性'] = ['瞬杀', 'None', '炎火'] >>> data 名字 等级 属性1 属性2 天赋 特性 0 艾欧里娅 100 自然 冰 29 瞬杀 1 泰格尔 80 电 战斗 16 None 2 布鲁克克 100 水 None 28 炎火Copy after login
data.loc[num of the row] = [value 1, value 2, ...], (note the difference with
.iloc difference)
>>> 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()function
>>> # 删除列, 需要指定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)
The above is the detailed content of Pandas read and modify excel operation strategy in Python (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
