Update:
Có ý kiến của bạn trong đầu, tôi thấy rằng tôi đã hiểu sai câu hỏi của bạn. Nếu bạn là tệp văn bản [
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
6] trông giống như sau:If you're text-file [
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
6] looks like follows:Car
Audi A4 10000
Audi A6 12000
Bus
VW Transporter 15000
Camper
VW California 20000
Vì vậy, sau mỗi
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
7, một lần ngắt dòng được thực hiện và giữa # Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
8 và # Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
9 là một tab, bạn có thể chạy mã sau:# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
Điều này dẫn đến bảng sau:
Category Model Price
0 Car Audi A4 10000.0
1 Car Audi A6 12000.0
2 Bus VW Transporter 15000.0
3 Camper VW California 20000.0
Câu trả lời cũ:
Tệp văn bản của bạn cần một cấu trúc cố định [ví dụ tất cả các giá trị được phân tách bằng một bảng hoặc ngắt dòng]. Sau đó, bạn có thể sử dụng phương thức
Category Model Price
0 Car Audi A4 10000.0
1 Car Audi A6 12000.0
2 Bus VW Transporter 15000.0
3 Camper VW California 20000.0
0 và xác định dấu phân cách bằng tay với Category Model Price
0 Car Audi A4 10000.0
1 Car Audi A6 12000.0
2 Bus VW Transporter 15000.0
3 Camper VW California 20000.0
1.Ví dụ, các tab là
Category Model Price
0 Car Audi A4 10000.0
1 Car Audi A6 12000.0
2 Bus VW Transporter 15000.0
3 Camper VW California 20000.0
2 và dòng ngắt Category Model Price
0 Car Audi A4 10000.0
1 Car Audi A6 12000.0
2 Bus VW Transporter 15000.0
3 Camper VW California 20000.0
3.Ví dụ, ví dụ
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
6 [liên kết] được cấu trúc bằng các tab và có thể đọc với:import pandas as pd
pd.read_csv['cars.txt', sep = '\t']
Có thể nhanh chóng tổ chức dữ liệu của chúng tôi thành một định dạng dễ đọc hơn, chẳng hạn như khi dữ liệu gây tranh cãi, có thể cực kỳ hữu ích để phân tích dữ liệu và lập kế hoạch cho các bước tiếp theo. Python cung cấp khả năng dễ dàng biến các loại dữ liệu bảng nhất định thành các bảng văn bản đơn giản được định dạng độc đáo và đó là chức năng bảng.tabulate function. Trước tiên chúng tôi cài đặt thư viện Tabulation bằng cách cài đặt PIP trong dòng lệnh:tabulate library using pip install in the command line:Cách sử dụng chức năng Tabulation để tạo các bảng được định dạng độc đáo trong Python
Ảnh của Fotis Fotopoulos trên unplashCài đặt bảng
pip install tabulate
Nhập chức năng bảng
Sau đó, chúng tôi nhập chức năng Tabulation từ thư viện Tabulation trong mã của chúng tôi:tabulate function from the tabulate library in our code:
from tabulate import tabulate
Và bây giờ chúng tôi đã sẵn sàng để sử dụng chức năng Tabulation!tabulate function!
Các loại dữ liệu bảng được hỗ trợ bởi Tabulation
Hàm Tabulation có thể chuyển đổi bất kỳ điều nào sau đây thành một bảng văn bản đơn giản dễ đọc: [từ tài liệu Tabulation]tabulate function can transform any of the following into an easy to read plain-text table: [from the tabulate documentation]
- Danh sách danh sách hoặc một số khác của Iterables
- Danh sách hoặc một điều khác có thể đi được của Dicts [phím là cột]
- Dict of Iterables [phím làm cột]
- Mảng numpy hai chiều
- Mảng bản ghi Numpy [tên dưới dạng cột]
- pandas.DataFrame
Danh sách danh sách
Ví dụ: nếu chúng ta có danh sách danh sách sau:
table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
Chúng ta có thể biến nó thành một bảng văn bản đơn giản dễ đọc hơn bằng cách sử dụng hàm bảng:tabulate function:
print[tabulate[table]]
Vì danh sách đầu tiên trong danh sách danh sách chứa tên của các cột là các phần tử của nó, chúng ta có thể đặt nó thành tên cột hoặc tên tiêu đề bằng cách truyền ‘firstrow, làm đối số cho tham số tiêu đề:headers parameter:
print[tabulate[table, headers='firstrow']]
Hàm Tabulation cũng chứa tham số bảng, cho phép chúng tôi cải thiện sự xuất hiện của bảng của chúng tôi bằng cách sử dụng giả đồ giả:tabulate function also contains a tablefmt parameter, which allows us to improve the appearance of our table using pseudo-graphics:
print[tabulate[table, headers='firstrow', tablefmt='grid']]
Tôi thích sử dụng đối số ‘Fancy_Grid cho bảng điều khiển:tablefmt:
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
0Từ điển của Iterables
Chúng ta có thể tạo cùng một bảng ở trên bằng cách sử dụng từ điển:
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
1Trong trường hợp của một từ điển, các phím sẽ là các tiêu đề cột và các giá trị sẽ là các phần tử của các cột đó. Chúng tôi chỉ định rằng các phím sẽ là các tiêu đề bằng cách truyền ‘phím, làm đối số cho tham số tiêu đề:keys will be the column headers, and the values will be the elements of those columns. We specify that the keys will be the headers by passing ‘keys’ as the argument for the headers parameter:
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
2Và tất nhiên chúng ta có thể sử dụng tham số bảng để cải thiện sự xuất hiện của bảng:tablefmt parameter to improve the table’s appearance:
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
3thêm một chỉ mục
Chúng tôi cũng có thể thêm một chỉ mục vào bảng của mình với tham số ShowIndex:index to our table with the showindex parameter:
Chúng ta có thể thêm một chỉ mục tùy chỉnh bằng cách chuyển trong một tham số ShowIndex. Ví dụ: nếu chúng ta muốn chỉ mục bắt đầu ở mức 1, chúng ta có thể chuyển trong một đối tượng phạm vi như đối số:showindex parameter. For example, if we want the index to start at 1, we can pass in a range object as the argument:
giá trị bị mất
Nếu chúng tôi xóa ‘Jennifer, khỏi từ điển thông tin trên, bảng của chúng tôi sẽ chứa một trường trống:info dictionary, our table will contain an empty field:
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
4Nếu có bất kỳ giá trị còn thiếu nào trong bảng của chúng tôi, chúng tôi có thể chọn những gì cần điền chúng bằng cách sử dụng tham số còn thiếu. Giá trị mặc định cho thiếu là một chuỗi trống. Nếu chúng ta thay đổi nó thành N/A, thì đây là những gì mà bảng của chúng ta sẽ trông như thế nào:missing values in our table, we can choose what to fill them in with using the missingval parameter. The default value for missingval is an empty string. If we change it to ‘N/A’, this is what what our table will look like:
# Read the file
data = pd.read_csv['cars.txt', names=['Model','Price','Category'], sep='\t']
# Transform the unstructured data
data.loc[[data['Price'].isnull[] == True], 'Category'] = data['Model']
data['Category'].fillna[method='ffill', inplace=True]
data.dropna[axis=0, subset=['Price'], inplace = True]
# Clean the dataframe
data.reset_index[drop=True, inplace=True]
data = data[['Category', 'Model', 'Price']]
print[data]
5Tôi hy vọng hướng dẫn này về cách dễ dàng tạo các bảng được định dạng độc đáo bằng cách sử dụng chức năng Tabulation là hữu ích. Cảm ơn bạn đã đọc!