Xóa tệp csv tiêu đề python

Trước tiên, bạn có thể truy xuất nội dung của tệp CSV đầu tiên trước khi áp dụng cho từng tệp và nối nó vào biến chuỗi có tiêu đề. Trong phần Áp dụng cho từng bạn có thể sử dụng một bước bỏ qua khác để bỏ qua tệp CSV

Dưới đây là một ví dụ về cách tiếp cận đó

1. Truy xuất mục đầu tiên trong Lấy nội dung tệp - CSV đầu tiên có hành động tiêu đề

outputs['Get_files_[properties_only]']?['body/value'][0]['{Identifier}']

2. Nối vào chuỗi [có tiêu đề]

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]

3. Sử dụng bỏ qua trong trường Chọn đầu ra từ các bước trước trong Áp dụng cho từng hành động

skip[outputs['Get_files_[properties_only]']?['body/value'], 1]

Để đọc tệp CSV không có tiêu đề, hãy sử dụng tham số tiêu đề và đặt thành “Không” trong phương thức read_csv[]

Giả sử sau đây là nội dung của tệp CSV được mở trong Microsoft Excel -

Lúc đầu, nhập thư viện cần thiết -

import pandas as pd

Tải dữ liệu từ tệp CSV vào Khung dữ liệu Pandas. Điều này cũng sẽ hiển thị các tiêu đề -

dataFrame = pd.read_csv["C:\Users\amit_\Desktop\SalesData.csv"]

Trong khi tải, hãy sử dụng tham số tiêu đề và đặt Không có để tải CSV không có tiêu đề –

pd.read_csv["C:\Users\amit_\Desktop\SalesData.csv", header=None]

Ví dụ

Sau đây là mã -

import pandas as pd

# Load data from a CSV file into a Pandas DataFrame
dataFrame = pd.read_csv["C:\Users\amit_\Desktop\SalesData.csv"]
print["\nReading the CSV file...\n",dataFrame]

# Load data from a CSV file and hide the header
dataFrame = pd.read_csv["C:\Users\amit_\Desktop\SalesData.csv", header=None]
print["\nReading the CSV file [without header]...\n",dataFrame]

đầu ra

Điều này sẽ tạo ra đầu ra sau -

Reading the CSV file...
       Car   Reg_Price   Units
0      BMW        2500     100
1    Lexus        3500      80
2     Audi        2500     120
3   Jaguar        2000      70
4  Mustang        2500     110

Reading the CSV file [without header]...
0 1 2
0      Car   Reg_Price Units
1      BMW        2500 100
2    Lexus        3500 80
3     Audi        2500 120
4   Jaguar        2000 70
5  Mustang        2500 110

Một thẻ đã tồn tại với tên chi nhánh được cung cấp. Nhiều lệnh Git chấp nhận cả tên thẻ và tên nhánh, vì vậy việc tạo nhánh này có thể gây ra hành vi không mong muốn. Bạn có chắc chắn muốn tạo nhánh này không?

Để xuất DataFrame của gấu trúc sang CSV mà không có chỉ mục [không có chỉ mục hàng], hãy sử dụng tham số

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
6 và để bỏ qua/xóa tiêu đề, hãy sử dụng tham số
concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
7 trên phương thức to_csv[]. Trong bài viết này, tôi sẽ giải thích cách xóa chỉ mục và tiêu đề trên tệp csv với các ví dụ. Lưu ý rằng phương thức to_csv[] cũng hỗ trợ một số tham số khác để ghi DataFrame của gấu trúc vào tệp CSV

1. Pandas sang CSV không có Chỉ mục và Tiêu đề

Theo mặc định, việc xuất DataFrame của gấu trúc sang CSV bao gồm tên cột trên hàng đầu tiên, chỉ mục hàng trên cột đầu tiên và ghi một tệp có dấu phân cách được phân tách bằng dấu phẩy vào các cột riêng biệt. gấu trúc. Khung dữ liệu. phương thức to_csv[] cung cấp các tham số để bỏ qua chỉ mục và tiêu đề trong khi viết

Trước tiên, hãy tạo một DataFrame với một vài hàng và cột cùng với tên chỉ mục và tiêu đề

________số 8

2. Pandas sang CSV không có chỉ mục

pandas DataFrame sang CSV không có chỉ mục có thể được thực hiện bằng cách sử dụng tham số

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
6 của phương thức to_csv[]. Với điều này, bạn có thể chỉ định bỏ qua chỉ mục trong khi ghi/xuất DataFrame sang tệp CSV


# Remover column header and index
df.to_csv["c:/tmp/courses.csv",index=False]

Viết tệp

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
9 không có chỉ mục

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
0

3. Pandas sang CSV không có tiêu đề

Để ghi DataFrame vào CSV mà không có tiêu đề cột [xóa tên cột], hãy sử dụng tham số

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
7 trên phương thức

# Remover column header and index
df.to_csv["c:/tmp/courses.csv",index=False]
1

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
2

Viết tệp

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
9 dưới dạng

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
3

4. Xuất không có chỉ mục và tiêu đề

Hãy kết hợp những điều này và xem cách bạn có thể sử dụng cả việc xóa tên cột và chỉ mục trên tệp CSV

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
4

Phần kết luận

Trong bài viết nhanh này, bạn đã học cách xóa tiêu đề không có tên cột và chỉ mục trong khi ghi DataFrame vào tệp CSV. Sử dụng tham số

concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
7 để xóa cột và sử dụng
concat[join[split[replace[base64ToString[body['Get_file_content_-_First_CSV_With_Header']['$content']], decodeUriComponent['%0D'], ''], decodeUriComponent['%0A']], decodeUriComponent['%0A']], decodeUriComponent['%0A']]
6 để xóa chỉ mục [không có chỉ mục hàng] trên phương pháp

# Remover column header and index
df.to_csv["c:/tmp/courses.csv",index=False]
1

Chủ Đề