Python 將 CSV 分割成多個文件的示例代碼
在本文中,我們將學習如何在 Python 中將一個 CSV 文件拆分為多個文件。 我們將使用 Pandas 創(chuàng)建一個 CSV 文件并將其拆分為多個其他文件。
使用 Pandas 在 Python 中創(chuàng)建 CSV 文件
要使用 Pandas 在 Python 中創(chuàng)建 CSV,必須首先通過命令行界面 (CLI) 安裝 Pandas。
pip install pandas
此命令將下載 Pandas 并將其安裝到您的本地計算機中。 使用 import
關鍵字,您可以輕松地將其導入到您當前的 Python 程序中。
讓我們驗證 Pandas 是否已安裝。
代碼示例:
import pandas as pd print("The Version of Pandas is: ", pd.__version__)
輸出:
The Version of Pandas is: 1.3.5
現在,讓我們創(chuàng)建一個 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 文件拆分為多個文件
我們已經成功創(chuàng)建了一個 CSV 文件。 讓我們將其拆分為多個文件,但可以使用不同的矩陣在列或行的基礎上拆分 CSV。
根據行拆分 CSV 文件
讓我們在 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 文件拆分為兩個多文件,student1.csv 和 student2.csv。 文件按行分隔; 第 0 到 3 行存儲在 student.csv 中,第 4 到 7 行存儲在 student2.csv 文件中。
根據列拆分 CSV 文件
借助 groupby()
函數,我們可以根據列矩陣拆分任何 CSV 文件。 groupby()
函數屬于 Pandas 庫,使用分組數據。
在這種情況下,我們根據性別對學生數據進行分組。
代碼示例:
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
總結
拆分數據是一種有用的數據分析技術,有助于理解和有效地排序數據。
在本文中,我們討論了如何使用 Pandas 庫創(chuàng)建 CSV 文件。 此外,我們還討論了兩種常見的數據拆分技術,行式數據拆分和列式數據拆分。
到此這篇關于Python 將 CSV 分割成多個文件的文章就介紹到這了,更多相關Python CSV 分割成多個文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!