網站首頁 編程語言 正文
根據csv表頭、列號讀取數據的實現
讀取csv文件
cvs數據截圖如下
設置index_col=0,目的是設置第一列name為index(索引),方便下面示例演示
data = pandas.read_csv(input1, index_col=0)
輸出結果
? ? ? ? price ?o_price ?date ?quan
name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
wood ? ?85.00 ? ?49.99 ?2006 ? 797
chair ?102.50 ? ?49.99 ?2006 ? 799
bed ? ? 77.00 ? ?49.99 ?2006 ? 795
lamp ? 162.50 ? ?49.99 ?2006 ? 800
sofa ? 699.99 ? 269.99 ?2002 ?3094
table ?602.00 ? 269.99 ?2002 ?3093
根據表頭獲取列數據
data[['o_price', 'quan']
# 或者
data.loc[:, ['o_price', 'quan']
輸出結果
? ? ? ?o_price ?quan
name ? ? ? ? ? ? ? ?
wood ? ? 49.99 ? 797
chair ? ?49.99 ? 799
bed ? ? ?49.99 ? 795
lamp ? ? 49.99 ? 800
sofa ? ?269.99 ?3094
table ? 269.99 ?3093
根據列號讀取列數據
data.iloc[:, [3, 4]]
輸出結果
? ? ? ?date ?quan
name ? ? ? ? ? ??
wood ? 2006 ? 797
chair ?2006 ? 799
bed ? ?2006 ? 795
lamp ? 2006 ? 800
sofa ? 2002 ?3094
table ?2002 ?3093
根據index名獲取行數據
data.loc[['wood', 'sofa'], :]
輸出結果
? ? ? ?price ?o_price ?date ?quan
name ? ? ? ? ? ? ? ? ? ? ? ? ? ??
wood ? 85.00 ? ?49.99 ?2006 ? 797
sofa ?699.99 ? 269.99 ?2002 ?3094
根據列號讀取行數據
data.iloc[[0, 1], :]
輸出結果
? ? ? ?price ?o_price ?date ?quan
name ? ? ? ? ? ? ? ? ? ? ? ? ? ??
wood ? ?85.0 ? ?49.99 ?2006 ? 797
chair ?102.5 ? ?49.99 ?2006 ? 799
iloc和loc區別
loc是根據dataframe的具體標簽選取列,而iloc是根據標簽所在的位置,從0開始計數。
讀取csv文件并輸出特定列
其實,最開始好不容易輸出了指定列,結果第二天不小心刪了什么東西,然后就一直報錯。
看上去和前一天能正常輸出的沒有什么差別。折騰了一天多總算是找到問題是什么了,是個很簡單的問題。
其實不是錯誤,只是因為選用的讀取方式不同,所以一直報錯。
源代碼如下
import csv
import pandas as pd?
sheet_name = "員工信息表.csv"
?
#數據文件有問題數據
with open(sheet_name,encoding = "utf-8",errors = "ignore") as f:
? ??
? ? #可通過列名讀取列值,表中有空值
? ? data= csv.DictReader(_.replace("\x00","") for _ in f)
? ? headers = next(data)
? ? print(headers)
? ? for row in data:
? ? ? ? print(row)
? ? ? ? if row['員工狀態'] == '2':
? ? ? ? ? ? print(row)
?
? ? #不可通過列名讀取列值,通過第幾列來讀取
? ? #data =csv.reader(_.replace("\x00","") for _ in f)
? ? headers = next(data)
? ? print(headers)
? ? for row in data:
? ? ? ? print(row)
? ? ? ? if row[12]=='2':
? ? ? ? ? ? print(row)
讀取csv文件需要采用:
with open(sheet_name,encoding = "utf-8",errors = "ignore") as f:
如果不加errors = "ignore"會報錯:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 0: invalid start byte
通過csv.reader讀取csv文件,然后使用列名row['員工狀態']輸出列值會報錯:
“TypeError: list indices must be integers or slices, not str”
根據這個報錯百度了好久,一直沒有找到解決方法。
雖然現在最終效果達到了,但是并不清楚具體原因。
源數據表里面問題好多啊,感覺需要先做數據清洗。唉!好難啊!
原文鏈接:https://blog.csdn.net/qq_33873431/article/details/105833727
相關推薦
- 2022-10-18 Qt實現柵格布局效果_C 語言
- 2022-05-07 Python中list列表的賦值方法及遇到問題處理_python
- 2023-04-23 Go之接口型函數用法_Golang
- 2022-02-28 Module not found: Error: Can't resolve 'sass-loade
- 2022-09-16 淺析python中5個帶key的內置函數_python
- 2022-01-12 錯誤:UnmappedTerms cannot be cast to org.elasticsear
- 2022-04-10 el-table中使用el-select,選擇后不刷新解決方法
- 2022-06-24 Windows?Server?2012遠程默認端口3389的修改方法_win服務器
- 最近更新
-
- 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同步修改后的遠程分支