網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
方法一
import os import pandas as pd path = 'data/train/' img_label_list=[] testList = os.listdir(path) for file in testList: label='aa' img_label_list.append([file, label]) df1 = pd.DataFrame(data=img_label_list, columns=['id', 'label']) df1.to_csv('result.csv',index=False)
方法二
import os import pandas as pd path = 'data/train/' img_list=[] lable_list=[] testList = os.listdir(path) for file in testList: img_list.append(file) label='aa' lable_list.append(label) img_label_list2 = list(zip(img_list, lable_list)) df3 = pd.DataFrame(data=img_label_list2, columns=['filepath', 'label']) df3.to_csv('result.csv',index=False)
方法三
import os import pandas as pd path = 'data/train/' img_list=[] lable_list=[] testList = os.listdir(path) for file in testList: img_list.append(file) label='aa' lable_list.append(label) df = pd.DataFrame({"filename": img_list, "label": lable_list}) df.to_csv('result.csv',index=False)
補(bǔ)充
當(dāng)然Pandas不僅可以實(shí)現(xiàn)CSV文件數(shù)據(jù)的保存,還能讀寫(xiě)CSV文件,下面是實(shí)現(xiàn)的核心代碼
使用pandas讀取CSV
import pandas as pd import csv if name == '__main__': # header=0——表示csv文件的第一行默認(rèn)為dataframe數(shù)據(jù)的行名稱(chēng), # index_col=0——表示使用第0列作為dataframe的行索引, # squeeze=True——表示如果文件只包含一列,則返回一個(gè)序列。 file_dataframe = pd.read_csv('../datasets/data_new_2/csv_file_name.csv', header=0, index_col=0, squeeze=True) # 結(jié)果:
寫(xiě)CSV
stu1 = [lid, k, pre_count_data[k]] # 打開(kāi)文件,寫(xiě)模式為追加'a' out = open('../results/write_file.csv', 'a', newline='') # 設(shè)定寫(xiě)入模式 csv_write = csv.writer(out, dialect='excel') # 寫(xiě)入具體內(nèi)容 csv_write.writerow(stu1)
原文鏈接:https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/123791632
相關(guān)推薦
- 2022-11-13 ASP.NET?MVC使用Session會(huì)話(huà)保持表單狀態(tài)_實(shí)用技巧
- 2022-09-03 Python實(shí)現(xiàn)計(jì)算AUC的示例代碼_python
- 2022-07-13 Android Canvas - StaticLayout 繪制多行文字
- 2022-07-28 Python?datacompy?找出兩個(gè)DataFrames不同的地方_python
- 2022-10-26 jQuery?基礎(chǔ)選擇器與屬性選擇器_jquery
- 2023-03-04 Qt利用tablewidget模擬手指實(shí)現(xiàn)滑動(dòng)_C 語(yǔ)言
- 2022-10-23 Linux服務(wù)器VPS的Windows?DD包詳細(xì)的制作教程_Linux
- 2022-09-29 關(guān)于numpy兩個(gè)array疊加操作詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤: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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支