網(wǎng)站首頁 編程語言 正文
使用pandas讀xlsx文件
- 讀取前n行數(shù)據(jù)
- 讀取指定數(shù)據(jù)(指定行指定列)
- 獲取文件行號和列標(biāo)題
- 將數(shù)據(jù)轉(zhuǎn)換為字典形式
import pandas as pd
#1.讀取前n行所有數(shù)據(jù)
df1=pd.read_excel('d1.xlsx')#讀取xlsx中的第一個sheet
data1=df1.head(10)#讀取前10行所有數(shù)據(jù)
data2=df1.values#list【】 相當(dāng)于一個矩陣,以行為單位
#data2=df.values() 報(bào)錯:TypeError: 'numpy.ndarray' object is not callable
print("獲取到所有的值:\n{0}".format(data1))#格式化輸出
print("獲取到所有的值:\n{0}".format(data2))
#2.讀取特定行特定列
data3=df1.iloc[0].values#讀取第一行所有數(shù)據(jù)
data4=df1.iloc[1,1]#讀取指定行列位置數(shù)據(jù):讀取(1,1)位置的數(shù)據(jù)
data5=df1.iloc[[1,2]].values#讀取指定多行:讀取第一行和第二行所有數(shù)據(jù)
data6=df1.iloc[:,[0]].values#讀取指定列的所有行數(shù)據(jù):讀取第一列所有數(shù)據(jù)
print("數(shù)據(jù):\n{0}".format(data3))
print("數(shù)據(jù):\n{0}".format(data4))
print("數(shù)據(jù):\n{0}".format(data5))
print("數(shù)據(jù):\n{0}".format(data6))
#3.獲取xlsx文件行號、列號
print("輸出行號列表{}".format(df1.index.values))#獲取所有行的編號:0、1、2、3、4
print("輸出列標(biāo)題{}".format(df1.columns.values))#也就是每列的第一個元素
#4.將xlsx數(shù)據(jù)轉(zhuǎn)換為字典
data=[]
for i in df1.index.values:#獲取行號的索引,并對其遍歷
#根據(jù)i來獲取每一行指定的數(shù)據(jù),并用to_dict轉(zhuǎn)成字典
row_data=df1.loc[i,['id','name','class','data','score',]].to_dict()
data.append(row_data)
print("最終獲取到的數(shù)據(jù)是:{0}".format(data))
#iloc和loc的區(qū)別:iloc根據(jù)行號來索引,loc根據(jù)index來索引。
#所以1,2,3應(yīng)該用iloc,4應(yīng)該有l(wèi)oc
數(shù)據(jù):d1.xlsx
id | name | class | data | score |
201901 | A | 1 | Jan-20 | 1.3 |
201902 | B | 2 | Mar-20 | 3.4 |
201903 | C | 3 | May-20 | 3.4 |
201904 | D | 1 | Jan-20 | 3.4 |
201905 | E | 1 | Feb-20 | 5.6 |
201906 | F | 1 | Mar-20 | 4.6 |
201907 | G | 1 | Feb-19 | 7.8 |
201908 | H | 2 | Apr-30 | 5.6 |
201909 | I | 3 | Jan-42 | 5.6 |
201910 | G | 4 | Mar-30 | 4.5 |
201911 | K | 5 | Apr-20 | 3.4 |
201912 | L | 6 | Apr-20 | 2.3 |
201913 | M | 4 | Mar-20 | 2.4 |
運(yùn)行結(jié)果展示
原文鏈接:https://blog.csdn.net/RitaAndWakaka/article/details/108366203
相關(guān)推薦
- 2022-03-14 Prefix must be in canonical form
- 2022-09-22 模擬實(shí)現(xiàn)vector
- 2022-06-07 Python利用capstone實(shí)現(xiàn)反匯編_python
- 2023-03-28 Python表示當(dāng)前時間的方法合集_python
- 2022-03-23 Unity3d實(shí)現(xiàn)跑馬燈廣播效果_C#教程
- 2022-07-06 Python?dataframe如何設(shè)置index_python
- 2023-04-18 Python設(shè)計(jì)模式中的策略模式詳解_python
- 2022-04-09 cas5 編譯安裝依賴時提示: Failure to find net.shibboleth.too
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 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)雅實(shí)現(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)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支