Làm cách nào để chia tệp csv trong python?

Trong bài viết này, chúng ta sẽ tìm hiểu cách tạo nhiều tệp CSV từ tệp CSV hiện có bằng Pandas. Khi chúng tôi nhập mã của mình vào sản xuất, chúng tôi sẽ cần xử lý việc chỉnh sửa các tệp dữ liệu của mình. Do dung lượng của file dữ liệu lớn nên chúng ta sẽ gặp nhiều vấn đề hơn nên chúng ta chia file này thành một số file nhỏ dựa trên một số tiêu chí như chia thành hàng, cột, giá trị cụ thể của cột, v.v.  

Trước tiên, hãy tạo một tệp CSV đơn giản và sử dụng nó cho tất cả các ví dụ bên dưới trong bài viết. Tạo tập dữ liệu bằng phương pháp khung dữ liệu của gấu trúc và sau đó lưu nó vào “Khách hàng. csv” hoặc chúng ta có thể tải tập dữ liệu hiện có bằng hàm Pandas read_csv[]

Python3




import pandas as pd

 

# initialise data dictionary.

data_dict= {'CustomerID': [1,pandas as pd0,pandas as pd2,pandas as pd4,pandas as pd6,pandas as pd8,# initialise data dictionary.0,# initialise data dictionary.2,# initialise data dictionary.4,# initialise data dictionary.6# initialise data dictionary.7

# initialise data dictionary.8

# initialise data dictionary.8data_dict0: [data_dict2,data_dict4,data_dict4,data_dict2,

=0data_dict2,data_dict4,data_dict2,data_dict2,

=0data_dict4,data_dict2# initialise data dictionary.7

# initialise data dictionary.8

# initialise data dictionary.8{6: [{8,'CustomerID'0,'CustomerID'2,'CustomerID'4,'CustomerID'6,'CustomerID'8,: [0,: [2,{8,'CustomerID'2# initialise data dictionary.7

# initialise data dictionary.8

# initialise data dictionary.810: [_______26,{8,__16,# initialise data dictionary.6,'CustomerID'6,,2,,4,

,6______97,'CustomerID'0,pandas as pd01# initialise data dictionary.7

# initialise data dictionary.8

# initialise data dictionary.8pandas as pd05: [16,pandas as pd09,pandas as pd11,pandas as pd13,pandas as pd15,pandas as pd17,: [0,pandas as pd21,

pandas as pd23pandas as pd24____9pandas as pd26pandas as pd27

 

pandas as pd28

pandas as pd29= pandas as pd31

 

pandas as pd32

pandas as pd33pandas as pd34pandas as pd35

 

pandas as pd36

pandas as pd37pandas as pd38

đầu ra

Tạo nhiều tệp CSV từ tệp CSV hiện có

Để thực hiện công việc của chúng tôi, chúng tôi sẽ thảo luận về các phương pháp khác nhau như sau

Phương pháp 1. Tách dựa trên hàng

Trong phương pháp này, chúng tôi sẽ chia một tệp CSV thành nhiều tệp CSV dựa trên các hàng

Python3




import pandas as pd

 

pandas as pd41

pandas as pd29= pandas as pd44pandas as pd34pandas as pd35

 

pandas as pd47

pandas as pd48= pandas as pd0

pandas as pd51= pandas as pd6

 

pandas as pd54 pandas as pd55pandas as pd56 pandas as pd57pandas as pd58

pandas as pd59pandas as pd60= pandas as pd62pandas as pd63pandas as pd64pandas as pd63pandas as pd66pandas as pd671pandas as pd69

pandas as pd59pandas as pd71____172pandas as pd73=pandas as pd75pandas as pd35

 

pandas as pd77= pandas as pd44pandas as pd80pandas as pd35

pandas as pd37pandas as pd83

 

pandas as pd84= pandas as pd44pandas as pd87pandas as pd35

pandas as pd37pandas as pd90

đầu ra

Phương pháp 2. Tách dựa trên cột

ví dụ 1

Sử dụng phương thức groupby[] của Pandas, chúng ta có thể tạo nhiều tệp CSV theo hàng. Để tạo một tệp, chúng ta có thể sử dụng phương thức to_csv[] của Pandas. Tại đây đã tạo hai tệp dựa trên các giá trị hàng giá trị “nam” và “nữ” của cột Giới tính cụ thể cho Điểm chi tiêu

Chủ Đề