Python 將 CSV 分割成多個(gè)文件的示例代碼
在本文中,我們將學(xué)習(xí)如何在 Python 中將一個(gè) CSV 文件拆分為多個(gè)文件。 我們將使用 Pandas 創(chuàng)建一個(gè) CSV 文件并將其拆分為多個(gè)其他文件。
使用 Pandas 在 Python 中創(chuàng)建 CSV 文件
要使用 Pandas 在 Python 中創(chuàng)建 CSV,必須首先通過命令行界面 (CLI) 安裝 Pandas。
pip install pandas
此命令將下載 Pandas 并將其安裝到您的本地計(jì)算機(jī)中。 使用 import 關(guān)鍵字,您可以輕松地將其導(dǎo)入到您當(dāng)前的 Python 程序中。
讓我們驗(yàn)證 Pandas 是否已安裝。
代碼示例:
import pandas as pd
print("The Version of Pandas is: ", pd.__version__)輸出:
The Version of Pandas is: 1.3.5
現(xiàn)在,讓我們創(chuàng)建一個(gè) CSV 文件。
代碼示例:
import pandas as pd
# create a data set
data_dict = {'Roll no': [1, 2, 3, 4, 5, 6, 7, 8],
'Gender': ["Male", "Female", "Female", "Male",
"Male", "Female", "Male", "Female"],
'CGPA': [3.5, 3.3, 2.7, 3.8, 2.4, 2.1, 2.9, 3.9],
'English': [76, 77, 85, 91, 49, 86, 66, 98],
'Mathematics': [78, 87, 54, 65, 90, 59, 63, 89],
'Programming': [99, 45, 68, 85, 60, 39, 55, 88]}
# create a data frame
data = pd.DataFrame(data_dict)
# convert the data frame into a csv file
data.to_csv("studesnts.csv")
# Print the output
print(data)輸出:
Roll no Gender CGPA English Mathematics Programming
0 1 Male 3.5 76 78 99
1 2 Female 3.3 77 87 45
2 3 Female 2.7 85 54 68
3 4 Male 3.8 91 65 85
4 5 Male 2.4 49 90 60
5 6 Female 2.1 86 59 39
6 7 Male 2.9 66 63 55
7 8 Female 3.9 98 89 88
在 Python 中將 CSV 文件拆分為多個(gè)文件
我們已經(jīng)成功創(chuàng)建了一個(gè) CSV 文件。 讓我們將其拆分為多個(gè)文件,但可以使用不同的矩陣在列或行的基礎(chǔ)上拆分 CSV。
根據(jù)行拆分 CSV 文件
讓我們?cè)?Python 中基于行拆分 CSV 文件。
代碼示例:
import pandas as pd
# read DataFrame
data = pd.read_csv("students.csv")
# number of csv files along with the row
k = 2
size = 4
for i in range(k):
df = data[size*i:size*(i+1)]
df.to_csv(f'students{i+1}.csv', index=False)
file1 = pd.read_csv("students1.csv")
print(file1)
print("\n")
file2 = pd.read_csv("students2.csv")
print(file2)輸出:
Roll no Gender CGPA English Mathematics Programming
0 1 Male 3.5 76 78 99
1 2 Female 3.3 77 87 45
2 3 Female 2.7 85 54 68
3 4 Male 3.8 91 65 85Roll no Gender CGPA English Mathematics Programming
4 5 Male 2.4 49 90 60
5 6 Female 2.1 86 59 39
6 7 Male 2.9 66 63 55
7 8 Female 3.9 98 89 88
上面的代碼將 students.csv 文件拆分為兩個(gè)多文件,student1.csv 和 student2.csv。 文件按行分隔; 第 0 到 3 行存儲(chǔ)在 student.csv 中,第 4 到 7 行存儲(chǔ)在 student2.csv 文件中。
根據(jù)列拆分 CSV 文件
借助 groupby() 函數(shù),我們可以根據(jù)列矩陣拆分任何 CSV 文件。 groupby() 函數(shù)屬于 Pandas 庫,使用分組數(shù)據(jù)。
在這種情況下,我們根據(jù)性別對(duì)學(xué)生數(shù)據(jù)進(jìn)行分組。
代碼示例:
import pandas as pd
# read DataFrame
data = pd.read_csv("students.csv")
for (gender), group in data.groupby(['Gender']):
group.to_csv(f'{gender} students.csv', index=False)
print(pd.read_csv("Male students.csv"))
print("\n")
print(pd.read_csv("Female students.csv"))輸出:
Roll no Gender CGPA English Mathematics Programming
0 1 Male 3.5 76 78 99
1 4 Male 3.8 91 65 85
2 5 Male 2.4 49 90 60
3 7 Male 2.9 66 63 55Roll no Gender CGPA English Mathematics Programming
0 2 Female 3.3 77 87 45
1 3 Female 2.7 85 54 68
2 6 Female 2.1 86 59 39
3 8 Female 3.9 98 89 88
總結(jié)
拆分?jǐn)?shù)據(jù)是一種有用的數(shù)據(jù)分析技術(shù),有助于理解和有效地排序數(shù)據(jù)。
在本文中,我們討論了如何使用 Pandas 庫創(chuàng)建 CSV 文件。 此外,我們還討論了兩種常見的數(shù)據(jù)拆分技術(shù),行式數(shù)據(jù)拆分和列式數(shù)據(jù)拆分。
到此這篇關(guān)于Python 將 CSV 分割成多個(gè)文件的文章就介紹到這了,更多相關(guān)Python CSV 分割成多個(gè)文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談python中的getattr函數(shù) hasattr函數(shù)
下面小編就為大家?guī)硪黄獪\談python中的getattr函數(shù) hasattr函數(shù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
多個(gè)版本的python共存時(shí)使用pip的正確做法
這篇文章主要介紹了多版本python共存時(shí)使用pip的正確做法,幫助有多個(gè)python版本需求的人可以正確的導(dǎo)包,感興趣的朋友可以了解下2020-10-10
TensorFlow梯度求解tf.gradients實(shí)例
今天小編就為大家分享一篇TensorFlow梯度求解tf.gradients實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python生成器實(shí)現(xiàn)簡(jiǎn)單"生產(chǎn)者消費(fèi)者"模型代碼實(shí)例
這篇文章主要介紹了Python生成器實(shí)現(xiàn)簡(jiǎn)單"生產(chǎn)者消費(fèi)者"模型代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
pycharm 復(fù)制代碼出現(xiàn)空格的解決方式
這篇文章主要介紹了pycharm 復(fù)制代碼出現(xiàn)空格的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
python保存log日志,實(shí)現(xiàn)用log日志畫圖
今天小編就為大家分享一篇python保存log日志,實(shí)現(xiàn)用log日志來畫圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
基于Python編寫PDF轉(zhuǎn)EPUB以及MOBI工具
當(dāng)我們需要在電子閱讀器上閱讀這些文檔時(shí),轉(zhuǎn)換為EPUB或MOBI格式會(huì)提供更好的閱讀體驗(yàn),所以本文將使用Python編寫一個(gè)PDF轉(zhuǎn)EPUB以及MOBI工具,需要的可以參考下2025-03-03

