Bởi vì bạn đã vượt qua các tên cột thay thế, điều này có nghĩa là trình phân tích cú pháp CSV đang diễn giải hàng đầu tiên dưới dạng hàng dữ liệu hợp lệ để bạn cần vượt qua skiprows=1
để bỏ qua tiêu đề của bạn, ngoài ra, bộ phân cách mặc định là dấu phẩy ,
nhưng có vẻ như dữ liệu của bạn là tab là tab hoặc đa không gian được phân tách để bạn có thể vượt qua sep='\t'
hoặc sep='\s+'
.
Không rõ liệu dữ liệu của bạn là tab hay không gian được phân tách nhưng những điều sau đây có hiệu quả với tôi không:
In [18]:
t="""Date Name Group Direction
2015-01-01 Smith.John - In
2015-01-01 Smith.Jan Claims Out
2015-01-01 - Claims In
2015-01-01 Smith.Jessica Other In"""
pd.read_csv[io.StringIO[t], names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\s+']
Out[18]:
Date AgentName Group Direction
0 2015-01-01 Smith.John - In
1 2015-01-01 Smith.Jan Claims Out
2 2015-01-01 - Claims In
3 2015-01-01 Smith.Jessica Other In
Vì vậy, tôi mong đợi
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
hoặc
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\s+']
để làm việc cho bạn
Trong bài viết này, chúng tôi sẽ thảo luận về cách đọc các tệp văn bản với gấu trúc trong Python. Trong Python, mô -đun PANDAS cho phép chúng tôi tải DataFrames từ các tệp bên ngoài và làm việc trên chúng. Bộ dữ liệu có thể nằm trong các loại tệp khác nhau.
Tệp văn bản được sử dụng:
Phương pháp 1: Sử dụng read_csv []
Chúng tôi sẽ đọc tệp văn bản với gấu trúc bằng hàm read_csv []. Cùng với tệp văn bản, chúng tôi cũng truyền phân tách dưới dạng một không gian duy nhất [‘] cho ký tự không gian bởi vì, đối với các tệp văn bản, ký tự không gian sẽ tách riêng từng trường. Có ba tham số chúng ta có thể chuyển đến hàm read_csv [].
Syntax:
data = pandas.read_csv [‘fileName.txt
Parameters:
- FileName.txt: Như tên cho thấy nó là tên của tệp văn bản mà chúng tôi muốn đọc dữ liệu. As the name suggests it is the name of the text file from which we want to read data.
- SEP: Đó là một trường phân cách. Trong tệp văn bản, chúng tôi sử dụng ký tự không gian [‘] làm dấu phân cách.: It is a separator field. In the text file, we use the space character[‘ ‘] as the separator.
- Tiêu đề: Đây là một lĩnh vực tùy chọn. Theo mặc định, nó sẽ lấy dòng đầu tiên của tệp văn bản làm tiêu đề. Nếu chúng ta sử dụng tiêu đề = không có thì nó sẽ tạo tiêu đề. This is an optional field. By default, it will take the first line of the text file as a header. If we use header=None then it will create the header.
- Tên: Chúng tôi có thể gán tên cột trong khi nhập tệp văn bản bằng cách sử dụng đối số tên.We can assign column names while importing the text file by using the names argument.
Ví dụ 1: & nbsp;
Python3
import
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
0pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
1pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
2 pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
3pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
4pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
5__1212171718pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
9pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\s+']
0Output:
Ví dụ 2:
Trong ví dụ 2, chúng tôi sẽ làm cho tiêu đề được nộp bằng không. Điều này sẽ tạo ra một tiêu đề mặc định trong đầu ra. Và lấy dòng đầu tiên của tệp văn bản làm mục nhập dữ liệu. Tên tiêu đề được tạo sẽ là một số bắt đầu từ 0.
Python3
import
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
0pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
1pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
2 pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
3pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
4pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
5__1212171718pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
9pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\s+']
0Output:
Ví dụ 2:
Trong ví dụ 2, chúng tôi sẽ làm cho tiêu đề được nộp bằng không. Điều này sẽ tạo ra một tiêu đề mặc định trong đầu ra. Và lấy dòng đầu tiên của tệp văn bản làm mục nhập dữ liệu. Tên tiêu đề được tạo sẽ là một số bắt đầu từ 0.
Python3
import
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
0pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
1pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
2 pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
3pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
4pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
5__1212171718Ví dụ 2:
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
9pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\s+']
0Output:
Trong ví dụ 2, chúng tôi sẽ làm cho tiêu đề được nộp bằng không. Điều này sẽ tạo ra một tiêu đề mặc định trong đầu ra. Và lấy dòng đầu tiên của tệp văn bản làm mục nhập dữ liệu. Tên tiêu đề được tạo sẽ là một số bắt đầu từ 0.
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
1pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
2 pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
3pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
4pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
5__12Syntax:
data=pandas.read_table['filename.txt', delimiter = ' ']
Example:
Python3
import
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
0pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
1pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
2 pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
3pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
4pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
5__1212171718pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
9pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\s+']
0Output:
Ví dụ 2:
Trong ví dụ 2, chúng tôi sẽ làm cho tiêu đề được nộp bằng không. Điều này sẽ tạo ra một tiêu đề mặc định trong đầu ra. Và lấy dòng đầu tiên của tệp văn bản làm mục nhập dữ liệu. Tên tiêu đề được tạo sẽ là một số bắt đầu từ 0.
Syntax:
data=pandas.read_fwf['filename.txt']
Example:
Python3
import
pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
0pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
1pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
2 pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
3pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
4pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
5__1212171718pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\t']
9pd.read_csv['C:\Users\Desktop\skills.txt', names=['Date','AgentName','Group','Direction'], skiprows=1, sep='\s+']
0Output: