網站首頁 編程語言 正文
用Python打開Excel數據,讀取時需要將”學號“和“ID"轉換成字符,以便后續操作
df = pd.read_excel(path, converters={'學號': str, 'ID': str})
以下是我的經歷來體會:
我在從Excel讀入python的數據時,發現讀出的是空值:
import pandas as pd df=pd.read_excel("D:/Python/05DataMineML/2022STU(1).xlsx") df
但是分明是有數據的,大概率出現的原因是sheetname
(表的名稱)出現了問題。
那就試試其他的方法:
下圖是Excel的表頭,共有115行數據。
方法一:使用usecols
#獲取字段的第一種寫法 import pandas as pd df=pd.read_excel('../05DataMineML/2022STU(1).xlsx',usecols=['學號','姓名','20220101','20220125','20220202','20220208','20220213','20220220','20220226','20220311','20220320','20220327','20220403','randscore'],index_col='姓名',sheet_name='2022STUMOOC') df.info()
index_col:指定作為表格的索引值
usecols:pandas讀取excel使用read_excel()中的usecols參數讀取指定的列
sheet_name:表名
重點:要使用usecols參數,sheet_name必須顯式寫出來。
方法二:使用numpy
#獲取字段的第二種寫法:使用numpy import pandas as pd import numpy as np df=pd.read_excel('../05DataMineML/2022STU(1).xlsx',converters={'學號':str},usecols=np.arange(3,16),index_col='姓名',sheet_name='2022STU') df.head()
這里就涉及converters:
converters={'學號':str}
:將學號轉換為字符類型,以便后續操作。
這里使用了usecols=np.arange(3,16)
方法三:使用切片區間
#獲取字段的第三種寫法:切片區間 import pandas as pd import numpy as np df=pd.read_excel('../05DataMineML/2022STUMOOC (1).xlsx',converters={'學號':str},usecols=("D:P"),index_col='姓名',sheet_name='2022STUMOOC') df
這里使用了usecols=("D:P")
,也就是使用了如下圖每列的序號值做切片
總結:
converters用法:轉換類型。比如將Excel數據一列從int變成str
usecols用法
usecols=[‘學號',‘姓名'] usecols=np.arange(3,16) usecols=(“D:P”)
原文鏈接:https://blog.csdn.net/wxfighting/article/details/123953013
相關推薦
- 2023-07-10 如何使用MyBatis框架實現對數據庫的增刪查改?
- 2022-12-13 C++?Boost?Format超詳細講解_C 語言
- 2022-04-25 JQuery實現Table的tr上移下移功能_jquery
- 2023-04-07 React?Mobx狀態管理工具的使用_React
- 2022-03-07 golang強制類型轉換和類型斷言_Golang
- 2022-07-07 圖解AVL樹數據結構輸入與輸出及實現示例_C 語言
- 2022-08-19 Android實現九宮格抽獎_Android
- 2023-07-02 Python配置文件管理之ini和yaml文件讀取的實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支