網站首頁 編程語言 正文
一、文件操作
- pandas內置了10多種數據源讀取函數,常見的就是CSV和EXCEL
- 使用read_csv方法讀取,結果為dataframe格式
- 在讀取csv文件時,文件名稱盡量是英文
- 讀取csv時,注意編碼,常用編碼為utf-8、gbk 、gbk2312和gb18030等
- 使用to_csv方法快速保存
1.1 csv文件讀寫
#讀取文件,以下兩種方式:
#使用pandas讀入需要處理的表格及sheet頁
import pandas as pd
df = pd.read_csv("test.csv",sheet_name='sheet1') #默認是utf-8編碼
#或者使用with關鍵字
with open("test.csv",encoding="utf-8")as df:
#按行遍歷
for row in df:
#修正
row = row.replace('陰性','0').replace('00.','0.')
...
print(row)
#將處理后的結果寫入新表
#建議用utf-8編碼或者中文gbk編碼,默認是utf-8編碼,index=False表示不寫出行索引
df.to_csv('df_new.csv',encoding='utf-8',index=False)
1.2 excel文件讀寫
#讀入需要處理的表格及sheet頁
df = pd.read_excel('測試.xlsx',sheet_name='test')
df = pd.read_excel(r'測試.xlsx') #默認讀入第一個sheet
#將處理后的結果寫入新表
df1.to_excel('處理后的數據.xlsx',index=False)
二、數據清洗
2.1 刪除空值
# 刪除空值行
# 使用索引
df.dropna(axis=0,how='all')#刪除全部值為空的行
df_1 = df[df['價格'].notna()] #刪除某一列值為空的行
df = df.dropna(axis=0,how='all',subset=['1','2','3','4','5'])# 這5列值均為空,刪除整行
df = df.dropna(axis=0,how='any',subset=['1','2','3','4','5'])#這5列值任何出現一個空,即刪除整行
2.2 刪除不需要的列
# 使用del, 一次只能刪除一列,不能一次刪除多列
del df['sample_1'] #修改源文件,且一次只能刪除一個
del df[['sample_1', 'sample_2']] #報錯
#使用drop,有兩種方法:
#使用列名
df = df.drop(['sample_1', 'sample_2'], axis=1) # axis=1 表示刪除列
df.drop(['sample_1', 'sample_2'], axis=1, inplace=True) # inplace=True, 直接從內部刪除
#使用索引
df.drop(df.columns[[0, 1, 2]], axis=1, inplace=True) # df.columns[ ] #直接使用索引查找列,刪除前3列
2.3 刪除不需要的行
#使用drop,有兩種方法:
#使用行名
df = df.drop(['行名1', '行名2']) # 默認axis=0 表示刪除行
df.drop(['行名1', '行名2'], inplace=True) # inplace=True, 直接從內部刪除
#使用索引
df.drop(df.index[[1, 3, 5]]) # df.index[ ]直接使用索引查找行,刪除1,3,5行
df = df[df.index % 2 == 0]#刪除偶數行
2.4 重置索引
#在刪除了行列數據后,造成索引混亂,可通過 reset_index重新生成連續索引
df.reset_index()#獲得新的index,原來的index變成數據列,保留下來
df.reset_index(drop=True)#不想保留原來的index,使用參數 drop=True,默認 False
df.reset_index(drop=True,inplace=True)#修改源文件
#使用某一列作為索引
df.set_index('column_name').head()
2.5 統計缺失
#每列的缺失數量
df.isnull().sum()
#每列缺失占比
df3.isnull().sum()/df.shape[0]
#每行的缺失數量
df3.isnull().sum(axis=1)
#每行缺失占比
df3.isnull().sum(axis=1)/df.shape[1]
2.6 排序
#按每行缺失值進行降序排序
df3.isnull().sum(axis=1).sort_values(ascending=False)
#按每列缺失率進行降序排序
(df.isnull().sum()/df.isnull().count()).sort_values(ascending=False)
原文鏈接:https://blog.csdn.net/weixin_46942725/article/details/125865706
相關推薦
- 2022-09-22 k8s 配置存儲之 Configmap & secret
- 2023-01-18 C#實現設置電腦顯示器參數_C#教程
- 2022-07-25 詳解docker進行數據掛載的三種模式_docker
- 2023-01-10 SpringEvent優雅解耦時連續兩個bug的解決方案_Golang
- 2022-02-17 如何通過一道題,全方位地考察自己,是否已經完美掌握了:this指向、作用域&作用域鏈、閉包、
- 2022-09-05 SpringBoot:整合JPA-Specifications動態查詢:
- 2022-10-30 Android?虛擬機中的內存分配與OOM問題詳解_Android
- 2022-07-21 Error: rsync: [sender] safe_read failed to read 4
- 最近更新
-
- 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同步修改后的遠程分支