- Phương pháp 1: Sử dụng gấu trúc
- Lập chỉ mục dựa trên danh sách của DataFrame
- Phương pháp 2: Lập chỉ mục dựa trên số nguyên với ILOC
- Phương pháp 3: Lập chỉ mục dựa trên tên với loc []
- Phương pháp 4: Sử dụng mô -đun CSV
- Sự kết luận
- Học gấu con cách thú vị bằng cách giải các câu đố mã
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 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]:Consier the following
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 –
- 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ộtimport 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? - Tất cả các giá trị trong 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']]
7?
Phương pháp 1: Sử dụng gấu trúc
Sử 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:
- Đọc tệp CSV bằng hàm gandas
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']]
8. - Lưu tất cả thông tin của quốc gia và vốn trong các biến độc lập bằng cách sử dụng -
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']]
9- Ngoài ra, bạn cũng có thể 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: object
0
- Ngoài ra, bạn cũng có thể 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: object
1- Ngoài ra, bạn cũng có thể 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: object
2
- Ngoài ra, bạn cũng có thể sử dụng
- Để hiển thị đồng thời các tên quốc gia và thủ đô của họ, bạn có thể liên kết hai cột,
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
3 và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
4, sử dụng hàm zip [] và sau đó hiển thị mỗi quốc gia cùng với vốn bằng cách sử dụng vòng lặp trên đối tượng bị nén. - Để hiển thị tất cả các giá trị trong cột dân số, bạn chỉ cần 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: object
5.
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: object
7. Nó kết hợp giá trị & nbsp;
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 DataFrame
Trong 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 ILOC
Phươ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.
- Phương pháp 4: Sử dụng mô -đun CSV
- Sự kết luận
- Học gấu con cách thú vị bằng cách giải các câu đố mã
- Nhìn nhanh vào các giải pháp [mỗi giải pháp vẫn ở trong 5-10 giây.]
- 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 đó.]
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 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']]
7?
Phương pháp 4: Sử dụng mô -đun CSV
Sự 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:
- Học gấu con cách thú vị bằng cách giải các câu đố mã
- Nhìn nhanh vào các giải pháp [mỗi giải pháp vẫn ở trong 5-10 giây.]
- 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 đó.]
- 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 -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ận
Học gấu con cách thú vị bằng cách giải các câu đố mã
- Nhìn nhanh vào các giải pháp [mỗi giải pháp vẫn ở trong 5-10 giây.]
- 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 đó.]
- 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?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
LinkedIn