Xem xét các bình luận và giải thích thêm, giả sử bạn biết tên của cột và bạn có chỉ mục vị trí, bạn có thể thử cách sau data = pd.read_csv(next(iglob('*.csv')))
row = data[data['creation date'] == 'Employee']
n = row.index[0]
data.drop(labels=list(range(n)), inplace=True)
Mục tiêu chính là tìm chỉ mục của hàng chứa giá trị 'Nhân viên'. Để đạt được điều đó, giả sử không có hàng nào khác chứa từ đó, bạn có thể lọc khung dữ liệu để khớp với giá trị được đề cập trong cột cụ thể. Sau đó, bạn trích xuất giá trị chỉ mục mà bạn sẽ sử dụng để tạo danh sách nhãn (được cung cấp chỉ mục vị trí) mà bạn sẽ loại bỏ khung dữ liệu, như @ MAK7 đã nêu trong câu trả lời của anh ấy. Xin chào, tôi có mã bên dưới để sắp xếp tệp csv chứa nhiều hàng cho mỗi người dùng và phải được sắp xếp theo một số cột và ngày, trong đó nếu cột 2, 3 và 5 giống nhau trong hàng hiện tại so với cột . các đầu ra được ghi vào hai tệp trên không trùng lặp và tệp còn lại để trùng lặp Dưới đây là một số mục nhập từ tập tin shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-23 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-26 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-18 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-23 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-24 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
Đây là kết quả shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
Đây là Mã import csv
entries = []
last_entry = [None, None, None]
check = [None, None, None]
duplicate_entries = []
with open('test.txt', 'r') as my_file:
for line in my_file:
columns = line.strip().split(',')
check[0] = columns[2]
check[1] = columns[3]
check[2] = columns[5]
if check != last_entry:
if columns[2] not in entries:
last_entry[0] = columns[2]
if columns[3] not in entries:
last_entry[1] = columns[3]
if columns[5] not in entries:
last_entry[2] = columns[5]
if columns[1] not in entries:
entries.append(columns)
else:
duplicate_entries.append(columns)
# writes entries to
with open('test_out.txt', 'w') as out_csv_file:
text_out = csv.writer(out_csv_file, delimiter=",")
for result in entries:
text_out.writerow(result)
# writing out duplicates from duplicate_entries
with open('test_dups.txt', 'w') as out_dups_file:
text_out = csv.writer(out_dups_file, delimiter=",")
for result in duplicate_entries:
text_out.writerow(result)
Vì tôi rất mới với python và lập trình nên tôi muốn biết cách tôi có thể cải thiện điều này và cũng như cách tôi có thể thực hiện điều này trong sqlite3 cho python Thao tác dữ liệu đề cập đến quá trình điều chỉnh dữ liệu để làm cho dữ liệu được tổ chức và dễ đọc hơn. Thông thường, có dữ liệu không sử dụng được và có thể ảnh hưởng đến những vấn đề quan trọng. Dữ liệu không cần thiết hoặc không chính xác nên được làm sạch và xóa Nguồn từ giải quyết. com [1] Có thể xóa một hoặc nhiều hàng/cột khỏi Khung dữ liệu Pandas theo nhiều cách. Trong số đó, phổ biến nhất là phương pháp # A more intuitive way df.drop(index=1) 5. Phương pháp này có vẻ khá dễ sử dụng, nhưng vẫn có một số thủ thuật bạn nên biết để tăng tốc độ phân tích dữ liệu của mìnhTrong bài viết này, bạn sẽ tìm hiểu 45 thủ thuật của Pandas để đối phó với các trường hợp sử dụng sau - Xóa một hàng
- Xóa nhiều hàng
- Xóa hàng dựa trên vị trí hàng và phạm vi tùy chỉnh
- Xóa một cột
- Xóa nhiều cột
- Xóa các cột dựa trên vị trí cột và phạm vi tùy chỉnh
- Làm việc với MultiIndex DataFrame
- Thực hiện thao tác tại chỗ với
# A more intuitive way df.drop(index=1) 7 - Loại bỏ lỗi với
# A more intuitive way df.drop(index=1) 8
Vui lòng kiểm tra Notebook để biết mã nguồn. Các hướng dẫn khác có sẵn từ Github Repo
1. Xóa một hàng Theo mặc định, Pandas # A more intuitive way df.drop(index=1) 5 sẽ xóa hàng dựa trên giá trị chỉ mục của chúng. Thông thường, giá trị chỉ mục là giá trị số nguyên dựa trên 0 trên mỗi hàng. Chỉ định một chỉ mục hàng sẽ xóa nó, ví dụ: xóa hàng có giá trị chỉ mục df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 0shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-23 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-26 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-18 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-23 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-24 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
0xóa một hàng bằng Pandas drop() (Ảnh của tác giả)Lưu ý rằng đối số df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 1 phải được đặt thành df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 2 để xóa các hàng (Trong Pandas # A more intuitive way df.drop(index=1) 5, df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 1 mặc định là df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 2, vì vậy có thể bỏ qua nó). Nếu df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 6 được chỉ định, thay vào đó nó sẽ xóa các cộtNgoài ra, một cách trực quan hơn để xóa một hàng khỏi DataFrame là sử dụng đối số df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 7# A more intuitive way df.drop(index=1) xóa một hàng bằng Pandas drop() (Ảnh của tác giả)2. Xóa nhiều hàngPandas # A more intuitive way df.drop(index=1) 5 có thể lấy một danh sách để xóa nhiều hàngdf.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) xóa nhiều hàng bằng Pandas drop() (Ảnh của tác giả)Tương tự, một cách trực quan hơn để xóa nhiều hàng là chuyển một danh sách tới đối số df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 7shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
2xóa nhiều hàng bằng Pandas drop() (Ảnh của tác giả)3. Xóa hàng dựa trên vị trí hàng và phạm vi tùy chỉnhCác giá trị chỉ mục DataFrame có thể không theo thứ tự tăng dần, đôi khi chúng có thể là bất kỳ giá trị nào khác, ví dụ: nhãn ngày giờ hoặc chuỗi. Đối với những trường hợp này, chúng ta có thể xóa các hàng dựa trên vị trí hàng của chúng, ví dụ: xóa hàng thứ 2, chúng ta có thể gọi shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
20 và chuyển nó vào đối số df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 7shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
5xóa hàng dựa trên vị trí hàng (Hình ảnh của tác giả)Để xóa hàng cuối cùng, chúng ta có thể sử dụng các phím tắt như shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
22 để xác định chỉ mục cuối cùngshift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
7xóa hàng dựa trên vị trí hàng (Hình ảnh của tác giả)Ví dụ, chúng ta cũng có thể sử dụng kỹ thuật lát cắt để chọn một loạt các hàng - Xóa 2 hàng cuối cùng
shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
23 - Xóa mọi hàng khác
shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
24 xóa hàng dựa trên vị trí hàng (Hình ảnh của tác giả)Nếu bạn muốn tìm hiểu thêm về kỹ thuật slice và cách sử dụng chỉ số hàng để chọn dữ liệu, bạn có thể xem bài viết này Cách sử dụng loc và iloc để chọn dữ liệu trong PandasMẹo và thủ thuật của Pandas để giúp bạn bắt đầu phân tích dữ liệuhướng tới khoa học dữ liệu. com 4. Xóa một cộtTương tự như xóa hàng, Pandas # A more intuitive way df.drop(index=1) 5 có thể được sử dụng để xóa cột bằng cách chỉ định đối số df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 1 thành df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 0import csv
entries = []
last_entry = [None, None, None]
check = [None, None, None]
duplicate_entries = []
with open('test.txt', 'r') as my_file:
for line in my_file:
columns = line.strip().split(',')
check[0] = columns[2]
check[1] = columns[3]
check[2] = columns[5]
if check != last_entry:
if columns[2] not in entries:
last_entry[0] = columns[2]
if columns[3] not in entries:
last_entry[1] = columns[3]
if columns[5] not in entries:
last_entry[2] = columns[5]
if columns[1] not in entries:
entries.append(columns)
else:
duplicate_entries.append(columns)
# writes entries to
with open('test_out.txt', 'w') as out_csv_file:
text_out = csv.writer(out_csv_file, delimiter=",")
for result in entries:
text_out.writerow(result)
# writing out duplicates from duplicate_entries
with open('test_dups.txt', 'w') as out_dups_file:
text_out = csv.writer(out_dups_file, delimiter=",")
for result in duplicate_entries:
text_out.writerow(result)
3xóa một cột bằng cách sử dụng Pandas drop() (Ảnh của tác giả)Một cách trực quan hơn để xóa một cột khỏi DataFrame là sử dụng đối số shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
28import csv
entries = []
last_entry = [None, None, None]
check = [None, None, None]
duplicate_entries = []
with open('test.txt', 'r') as my_file:
for line in my_file:
columns = line.strip().split(',')
check[0] = columns[2]
check[1] = columns[3]
check[2] = columns[5]
if check != last_entry:
if columns[2] not in entries:
last_entry[0] = columns[2]
if columns[3] not in entries:
last_entry[1] = columns[3]
if columns[5] not in entries:
last_entry[2] = columns[5]
if columns[1] not in entries:
entries.append(columns)
else:
duplicate_entries.append(columns)
# writes entries to
with open('test_out.txt', 'w') as out_csv_file:
text_out = csv.writer(out_csv_file, delimiter=",")
for result in entries:
text_out.writerow(result)
# writing out duplicates from duplicate_entries
with open('test_dups.txt', 'w') as out_dups_file:
text_out = csv.writer(out_dups_file, delimiter=",")
for result in duplicate_entries:
text_out.writerow(result)
5xóa một cột bằng Pandas drop() (Ảnh của tác giả)5. Xóa nhiều cộtTương tự, chúng ta có thể chuyển một danh sách để xóa nhiều cột import csv
entries = []
last_entry = [None, None, None]
check = [None, None, None]
duplicate_entries = []
with open('test.txt', 'r') as my_file:
for line in my_file:
columns = line.strip().split(',')
check[0] = columns[2]
check[1] = columns[3]
check[2] = columns[5]
if check != last_entry:
if columns[2] not in entries:
last_entry[0] = columns[2]
if columns[3] not in entries:
last_entry[1] = columns[3]
if columns[5] not in entries:
last_entry[2] = columns[5]
if columns[1] not in entries:
entries.append(columns)
else:
duplicate_entries.append(columns)
# writes entries to
with open('test_out.txt', 'w') as out_csv_file:
text_out = csv.writer(out_csv_file, delimiter=",")
for result in entries:
text_out.writerow(result)
# writing out duplicates from duplicate_entries
with open('test_dups.txt', 'w') as out_dups_file:
text_out = csv.writer(out_dups_file, delimiter=",")
for result in duplicate_entries:
text_out.writerow(result)
6xóa nhiều cột bằng Pandas drop() (Ảnh của tác giả)Một cách trực quan hơn để xóa nhiều cột là chuyển một danh sách tới đối số shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
28import csv
entries = []
last_entry = [None, None, None]
check = [None, None, None]
duplicate_entries = []
with open('test.txt', 'r') as my_file:
for line in my_file:
columns = line.strip().split(',')
check[0] = columns[2]
check[1] = columns[3]
check[2] = columns[5]
if check != last_entry:
if columns[2] not in entries:
last_entry[0] = columns[2]
if columns[3] not in entries:
last_entry[1] = columns[3]
if columns[5] not in entries:
last_entry[2] = columns[5]
if columns[1] not in entries:
entries.append(columns)
else:
duplicate_entries.append(columns)
# writes entries to
with open('test_out.txt', 'w') as out_csv_file:
text_out = csv.writer(out_csv_file, delimiter=",")
for result in entries:
text_out.writerow(result)
# writing out duplicates from duplicate_entries
with open('test_dups.txt', 'w') as out_dups_file:
text_out = csv.writer(out_dups_file, delimiter=",")
for result in duplicate_entries:
text_out.writerow(result)
8xóa nhiều cột bằng Pandas drop() (Ảnh của tác giả)6. Xóa các cột dựa trên vị trí cột và phạm vi tùy chỉnhChúng ta có thể xóa một cột dựa trên vị trí cột của nó, ví dụ: xóa cột thứ 2, chúng ta có thể gọi shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
50 và chuyển nó vào đối số shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
28# A more intuitive way df.drop(index=1) 0xóa cột dựa trên vị trí cột (Ảnh của tác giả)Để xóa cột cuối cùng, chúng ta có thể sử dụng các phím tắt như shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
22 để xác định chỉ mục cuối cùng# A more intuitive way df.drop(index=1) 1xóa cột dựa trên vị trí cột (Ảnh của tác giả)Tương tự, chúng ta cũng có thể sử dụng kỹ thuật lát cắt để chọn một dãy cột, chẳng hạn - Xóa 2 cột cuối cùng
shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
53 - Xóa mọi cột khác
shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
54 xóa cột dựa trên vị trí cột (Ảnh của tác giả)7. Làm việc với Multi IndexKhung dữ liệu MultiIndex (còn được gọi là chỉ mục phân cấp) cho phép chúng tôi có nhiều cột đóng vai trò là mã định danh hàng và nhiều hàng đóng vai trò là mã định danh tiêu đề (hình ảnh của tác giả)Khi gọi Pandas # A more intuitive way df.drop(index=1) 5 trên Khung dữ liệu MultiIndex, nó sẽ xóa chỉ mục và cột cấp 0 theo mặc định# A more intuitive way df.drop(index=1) 2Pandas drop() trong MultiIndex (hình ảnh của tác giả)Để chỉ định mức bị xóa, chúng ta có thể đặt đối số shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
56# A more intuitive way df.drop(index=1) 3Pandas drop() trong MultiIndex (hình ảnh của tác giả)Trong một số trường hợp, chúng tôi muốn xóa một tổ hợp chỉ mục hoặc cột cụ thể. Để làm điều đó, chúng ta có thể truyền một tuple cho đối số df.drop([1,2])# It's equivalent to df.drop(labels=[1,2]) 7 hoặc shift1,2021-02-14 06:35:00,J,P2,***USER16-J-P2,USER16
shift1,2021-02-15 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-17 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-18 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-19 06:35:00,J,P1,***USER16-J-P1,USER16
shift1,2021-02-22 07:35:00,J9,P2,***USER16-J9-P2,USER16
shift1,2021-02-25 06:35:00,J,P3,***USER16-J-P3,USER16
shift1,2021-02-27 06:35:00,J,P2,***USER16-J-P2,USER16
...
shift1,2021-02-15 14:35:00,S,P1,***USER23-S-P1,USER23
shift1,2021-02-17 07:35:00,J9,P3,***USER23-J9-P3,USER23
shift1,2021-02-19 06:35:00,J,P1,***USER23-J-P1,USER23
shift1,2021-02-19 22:55:00,N,P1,***USER23-N-P1,USER23
shift1,2021-02-21 06:35:00,J,P3,***USER23-J-P3,USER23
shift1,2021-02-22 22:55:00,N,P2,***USER23-N-P2,USER23
shift1,2021-02-26 07:35:00,J9,P2,***USER23-J9-P2,USER23
28# A more intuitive way df.drop(index=1) 4Pandas drop() trong MultiIndex (hình ảnh của tác giả)Nếu bạn muốn tìm hiểu thêm về cách truy cập dữ liệu trong MultiIndex DataFrame, vui lòng xem bài viết này Truy cập dữ liệu trong Khung dữ liệu MultiIndex trong Pandashướng tới khoa học dữ liệu. com 8. Thực hiện thao tác tại chỗ với # A more intuitive way df.drop(index=1) 7Theo mặc định, Pandas # A more intuitive way df.drop(index=1) 5 trả về một bản sao của kết quả mà không ảnh hưởng đến Khung dữ liệu đã cho. Chúng tôi có thể đặt đối số # A more intuitive way df.drop(index=1) 7 để thực hiện thao tác tại chỗ để tránh gán lại bổ sung và giảm mức sử dụng bộ nhớ9. Loại bỏ lỗi với # A more intuitive way df.drop(index=1) 8Bạn có thể nhận thấy rằng Pandas # A more intuitive way df.drop(index=1) 5 sẽ đưa ra lỗi khi các hàng hoặc cột đã cho không tồn tại. Chúng ta có thể đặt đối số # A more intuitive way df.drop(index=1) 8 để chặn lỗiPhần kết luậnTrong bài viết này, chúng tôi đã đề cập đến 9 trường hợp sử dụng về việc xóa các hàng và cột bằng cách sử dụng Pandas # A more intuitive way df.drop(index=1) 5. Bản thân phương pháp này rất dễ sử dụng và nó là một trong những phương pháp được yêu thích hàng đầu để thao tác dữ liệu trong Tiền xử lý dữ liệuCảm ơn vì đã đọc. Vui lòng xem Notebook để biết mã nguồn và tiếp tục theo dõi nếu bạn quan tâm đến khía cạnh thực tế của máy học. Các hướng dẫn khác có sẵn từ Github Repo |