網(wǎng)站首頁 編程語言 正文
用Python打開Excel數(shù)據(jù),讀取時需要將”學(xué)號“和“ID"轉(zhuǎn)換成字符,以便后續(xù)操作
df = pd.read_excel(path, converters={'學(xué)號': str, 'ID': str})
以下是我的經(jīng)歷來體會:
我在從Excel讀入python的數(shù)據(jù)時,發(fā)現(xiàn)讀出的是空值:
import pandas as pd df=pd.read_excel("D:/Python/05DataMineML/2022STU(1).xlsx") df
但是分明是有數(shù)據(jù)的,大概率出現(xiàn)的原因是sheetname
(表的名稱)出現(xiàn)了問題。
那就試試其他的方法:
下圖是Excel的表頭,共有115行數(shù)據(jù)。
方法一:使用usecols
#獲取字段的第一種寫法 import pandas as pd df=pd.read_excel('../05DataMineML/2022STU(1).xlsx',usecols=['學(xué)號','姓名','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參數(shù)讀取指定的列
sheet_name:表名
重點:要使用usecols參數(shù),sheet_name必須顯式寫出來。
方法二:使用numpy
#獲取字段的第二種寫法:使用numpy import pandas as pd import numpy as np df=pd.read_excel('../05DataMineML/2022STU(1).xlsx',converters={'學(xué)號':str},usecols=np.arange(3,16),index_col='姓名',sheet_name='2022STU') df.head()
這里就涉及converters:
converters={'學(xué)號':str}
:將學(xué)號轉(zhuǎn)換為字符類型,以便后續(xù)操作。
這里使用了usecols=np.arange(3,16)
方法三:使用切片區(qū)間
#獲取字段的第三種寫法:切片區(qū)間 import pandas as pd import numpy as np df=pd.read_excel('../05DataMineML/2022STUMOOC (1).xlsx',converters={'學(xué)號':str},usecols=("D:P"),index_col='姓名',sheet_name='2022STUMOOC') df
這里使用了usecols=("D:P")
,也就是使用了如下圖每列的序號值做切片
總結(jié):
converters用法:轉(zhuǎn)換類型。比如將Excel數(shù)據(jù)一列從int變成str
usecols用法
usecols=[‘學(xué)號',‘姓名'] usecols=np.arange(3,16) usecols=(“D:P”)
原文鏈接:https://blog.csdn.net/wxfighting/article/details/123953013
相關(guān)推薦
- 2022-11-21 小白也能看懂的Redis遍歷鍵和數(shù)據(jù)庫管理詳解_Redis
- 2023-05-16 golang-gorm自動建表問題_Golang
- 2022-11-01 go語言中for?range使用方法及避坑指南_Golang
- 2022-09-18 K8s實戰(zhàn)教程之容器和?Pods資源分配問題_云其它
- 2022-06-04 Android實現(xiàn)懸浮窗效果_Android
- 2023-01-23 React新擴展函數(shù)setState與lazyLoad及hook介紹_React
- 2022-06-15 python多線程實現(xiàn)動態(tài)圖繪制_python
- 2022-08-31 C語言詳解實現(xiàn)猜數(shù)字游戲步驟_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支