Hướng dẫn how do you read a column of data in a csv file in python? - làm cách nào để đọc một cột dữ liệu trong tệp csv trong python?
Show
Vấn đề: Đưa ra một tệp CSV, làm thế nào để chỉ đọc (các) cột cụ thể từ tệp CSV? (Đọc một cột cụ thể từ tệp CSV sẽ mang lại tất cả các giá trị hàng liên quan đến cột đó.)Given a CSV file, how to read only specific column(s) from the csv file? (Reading a specific column from a csv file will yield all the row values pertaining to that column.) Ví dụ: Consier tệp import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])3 file ( import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])4): Country,Capital,Population,Area Germany,Berlin,"84,267,549","348,560" France,Paris,"65,534,239","547,557" Spain,Madrid,"46,787,468","498,800" Italy,Rome,"60,301,346","294,140" India,Delhi,"1,404,495,187","2,973,190" USA,Washington,"334,506,463","9,147,420" China,Beijing,"1,449,357,022","9,388,211" Poland,Warsaw,"37,771,789","306,230" Russia,Moscow,"146,047,418","16,376,870" England,London,"68,529,747","241,930" Câu hỏi: Làm thế nào bạn sẽ đọc tệp CSV trên và hiển thị các cột sau - How will you read the above csv file and display the following columns –
Phương pháp 1: Sử dụng gấu trúcSử dụng thư viện Pandas có lẽ là lựa chọn tốt nhất nếu bạn đang xử lý các tệp CSV. Bạn có thể dễ dàng đọc tệp CSV và lưu trữ toàn bộ cột trong một biến. Code: import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population']) Output: Germany Berlin France Paris Spain Madrid Italy Rome India Delhi USA Washington China Beijing Poland Warsaw Russia Moscow England London 0 84,267,549 1 65,534,239 2 46,787,468 3 60,301,346 4 1,404,495,187 5 334,506,463 6 1,449,357,022 7 37,771,789 8 146,047,418 9 68,529,747 Name: Population, dtype: object Giải trình:
Trivia ________ 26 & nbsp; là một chức năng tích hợp trong Python lấy một số lượng tùy ý & nbsp; iterables & nbsp; và liên kết chúng thành một đối tượng Germany Berlin France Paris Spain Madrid Italy Rome India Delhi USA Washington China Beijing Poland Warsaw Russia Moscow England London 0 84,267,549 1 65,534,239 2 46,787,468 3 60,301,346 4 1,404,495,187 5 334,506,463 6 1,449,357,022 7 37,771,789 8 146,047,418 9 68,529,747 Name: Population, dtype: object6 is a built-in function in Python that takes an arbitrary number of iterables and binds them into a single iterable, a Germany Berlin France Paris Spain Madrid Italy Rome India Delhi USA Washington China Beijing Poland Warsaw Russia Moscow England London 0 84,267,549 1 65,534,239 2 46,787,468 3 60,301,346 4 1,404,495,187 5 334,506,463 6 1,449,357,022 7 37,771,789 8 146,047,418 9 68,529,747 Name: Population, dtype: object7 object. It combines the Germany Berlin France Paris Spain Madrid Italy Rome India Delhi USA Washington China Beijing Poland Warsaw Russia Moscow England London 0 84,267,549 1 65,534,239 2 46,787,468 3 60,301,346 4 1,404,495,187 5 334,506,463 6 1,449,357,022 7 37,771,789 8 146,047,418 9 68,529,747 Name: Population, dtype: object8 value of each iterable argument into a tuple. Read more about zip() here. Lập chỉ mục dựa trên danh sách của DataFrame List-Based Indexing of a DataFrameTrong trường hợp bạn không thoải mái với việc sử dụng Germany Berlin France Paris Spain Madrid Italy Rome India Delhi USA Washington China Beijing Poland Warsaw Russia Moscow England London 0 84,267,549 1 65,534,239 2 46,787,468 3 60,301,346 4 1,404,495,187 5 334,506,463 6 1,449,357,022 7 37,771,789 8 146,047,418 9 68,529,747 Name: Population, dtype: object6 để hiển thị nhiều cột cùng một lúc, bạn có một tùy chọn khác. Bạn chỉ có thể sử dụng lập chỉ mục dựa trên danh sách để hoàn thành mục tiêu của mình. Lập chỉ mục dựa trên danh sách là một kỹ thuật cho phép bạn vượt qua nhiều tên cột dưới dạng A & NBSP; Danh sách & NBSP; trong bộ chọn khung vuông. Example: import pandas as pd df = pd.read_csv("countries.csv") print() print(df[['Country', 'Capital']]) Output: Country Capital 0 Germany Berlin 1 France Paris 2 Spain Madrid 3 Italy Rome 4 India Delhi 5 USA Washington 6 China Beijing 7 Poland Warsaw 8 Russia Moscow 9 England London Phương pháp 2: Lập chỉ mục dựa trên số nguyên với ILOCPhương pháp 3: Lập chỉ mục dựa trên tên với loc ()The idea here is to use the import pandas as pd df = pd.read_csv("countries.csv") print() print(df[['Country', 'Capital']])0 to access individual columns from the DataFrame using indexing. Note that the first column always has the index 0, while the second column has index 1, and so on.
import pandas as pd data = pd.read_csv('countries.csv') country = data.iloc[:, 0].values capital = data.iloc[:, 1].values population = data.iloc[:, 2].values # displaying selected columns print(data[['Country', 'Capital']]) print() # displaying a single column (Population) print(population) Output: Country Capital 0 Germany Berlin 1 France Paris 2 Spain Madrid 3 Italy Rome 4 India Delhi 5 USA Washington 6 China Beijing 7 Poland Warsaw 8 Russia Moscow 9 England London ['84,267,549' '65,534,239' '46,787,468' '60,301,346' '1,404,495,187' '334,506,463' '1,449,357,022' '37,771,789' '146,047,418' '68,529,747'] Ví dụ: Consier tệp import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])3 sau (import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])4):Câu hỏi: Làm thế nào bạn sẽ đọc tệp CSV trên và hiển thị các cột sau - Cột import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])5 cùng với cột import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])6? import pandas as pd data = pd.read_csv('countries.csv') val = data.loc[:, ['Country', 'Capital']] print(val) Output: Country Capital 0 Germany Berlin 1 France Paris 2 Spain Madrid 3 Italy Rome 4 India Delhi 5 USA Washington 6 China Beijing 7 Poland Warsaw 8 Russia Moscow 9 England London Tất cả các giá trị trong cột Phương pháp 4: Sử dụng mô -đun CSVSự kết luận import csv population = [] with open('countries.csv', newline='', encoding='utf-8-sig') as csvfile: data = csv.DictReader(csvfile) for r in data: print("Country", ":", "Capital") # append values from population column to population list population.append(r['Population']) # displaying specific columns (Country and Capital) print(r['Country'], ":", r['Capital']) # display the population list print(population) Output: import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])0 Explanation:
Câu hỏi: Làm thế nào bạn sẽ đọc tệp CSV trên và hiển thị các cột sau -Here’s a quick look at how the Country Capital 0 Germany Berlin 1 France Paris 2 Spain Madrid 3 Italy Rome 4 India Delhi 5 USA Washington 6 China Beijing 7 Poland Warsaw 8 Russia Moscow 9 England London5 class looks like: import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])1 Output: import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])2 Cột import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])5 cùng với cột import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])6? Sự kết luậnHọc gấu con cách thú vị bằng cách giải các câu đố mã
Cột import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])5 cùng với cột import pandas as pd df = pd.read_csv("countries.csv") country = df['Country'] # or # country = df.Country capital = df['Capital'] # or # capital = df.Capital # displaying selected columns (Country and Capital) for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country) print() print(df['Population'])6?subscribe and stay tuned for more interesting tutorials. Happy learning! Học gấu con cách thú vị bằng cách giải các câu đố mãNếu bạn muốn tăng kỹ năng gấu trúc của mình, hãy xem xét kiểm tra cuốn sách học tập dựa trên câu đố của tôi Coffee Break Pandas (Amazon Link). Nó chứa 74 câu đố gấu trúc thủ công bao gồm các giải thích. Bằng cách giải quyết từng câu đố, bạn sẽ nhận được một số điểm đại diện cho cấp độ kỹ năng của bạn trong gấu trúc. Bạn có thể trở thành một Grandmaster Gandas không? Coffee Break Pandas cung cấp một cách tiếp cận dựa trên niềm vui để làm chủ khoa học dữ liệu và một trải nghiệm học tập thực sự được chơi game. Tôi là một blogger Python chuyên nghiệp và người tạo nội dung. Tôi đã xuất bản nhiều bài báo và tạo ra các khóa học trong một khoảng thời gian. Hiện tại tôi đang làm việc như một freelancer toàn thời gian và tôi có kinh nghiệm trong các lĩnh vực như Python, AWS, DevOps và Mạng. Bạn có thể liên lạc với tôi @: Upwork LinkedIn Làm cách nào để đọc một cột trong tệp CSV?Điều này có thể được thực hiện với sự trợ giúp của phương thức pandas.Read_csv (). Chúng tôi sẽ vượt qua tham số đầu tiên dưới dạng tệp CSV và tham số thứ hai danh sách các cột cụ thể trong từ khóa usecols. Nó sẽ trả về dữ liệu của tệp CSV của các cột cụ thể.pandas. read_csv() method. We will pass the first parameter as the CSV file and the second parameter the list of specific columns in the keyword usecols. It will return the data of the CSV file of specific columns.
Làm cách nào để đọc một cột cụ thể từ DataFrame trong Python?Bạn có thể sử dụng các chức năng LỘC và ILOC để truy cập các cột trong khung dữ liệu gấu trúc.Hãy xem làm thế nào.Nếu chúng tôi muốn truy cập một cột nhất định trong DataFrame của chúng tôi, ví dụ như cột Lớp, chúng tôi chỉ cần sử dụng hàm LỘC và chỉ định tên của cột để lấy lại.use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.
Làm cách nào để đọc một hàng cụ thể trong tệp CSV trong Python?Sử dụng độc giả.. Bước 1: Để đọc các hàng trong Python, trước tiên, chúng ta cần tải tệp CSV trong một đối tượng.Vì vậy, để tải tệp CSV vào một đối tượng sử dụng phương thức Open () .. Bước 2: Tạo đối tượng đầu đọc bằng cách chuyển đối tượng tệp được tạo ở trên cho hàm đầu đọc .. Bước 3: Sử dụng cho vòng lặp trên đối tượng đầu đọc để có được mỗi hàng .. Làm thế nào để bạn đọc một cột từ một tệp văn bản trong Python?Để đọc một tệp văn bản trong Python, bạn làm theo các bước sau: Đầu tiên, hãy mở một tệp văn bản để đọc bằng cách sử dụng hàm Open ().Thứ hai, đọc văn bản từ tệp văn bản bằng cách sử dụng phương thức read (), readline () hoặc readlines () của đối tượng tệp.Thứ ba, đóng tệp bằng phương thức đóng tệp (). |