![]() |
|
[Tut] How to Read Specific Columns from CSV File in Python - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Read Specific Columns from CSV File in Python (/thread-99375.html) |
[Tut] How to Read Specific Columns from CSV File in Python - xSicKxBot - 05-12-2022 How to Read Specific Columns from CSV File in Python Rate this post Table of Contents
A Quick Glance at The Solutions [Each solution stays for 5-10 secs.]Problem: 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.) Example: Consier the following Code: csvCode: countries.csvCode: 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"Question: How will you read the above csv file and display the following columns
[b]Method 1: Using Pandas[/b] Using the Pandas library is probably the best option if you are dealing with csv files. You can easily read a csv file and store an entire column within a variable. Code: 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: Code: 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: objectExplanation:
TRIVIA Code: zip()Code: zipCode: n-thRead more about zip() here. [b]➤ List-Based Indexing of a DataFrame[/b] In case you are not comfortable with using Code: zip()List-based indexing is a technique that allows you to pass multiple column names as a list within the square-bracket selector. Example: Code: import pandas as pd df = pd.read_csv("countries.csv")
print()
print(df[['Country', 'Capital']])Output: Code: 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 LondonApproach: The idea here is to use the Code: df.iloc[rows, columns].values
Code: 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: Code: 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']Instead of selecting the columns by their index, you can also select them by their name using the Code: df.loc[]The following example shows how to select the columns Code: CountryCode: CapitalCode: import pandas as pd data = pd.read_csv('countries.csv')
val = data.loc[:, ['Country', 'Capital']]
print(val)Output: Code: 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 LondonRelated Tutorial: Slicing Data from a Pandas DataFrame using .loc and .iloc [b]Method 4: Using csv Module[/b] Code: csvCode: 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: Code: Country : Capital
Germany : Berlin
Country : Capital
France : Paris
Country : Capital
Spain : Madrid
Country : Capital
Italy : Rome
Country : Capital
India : Delhi
Country : Capital
USA : Washington
Country : Capital
China : Beijing
Country : Capital
Poland : Warsaw
Country : Capital
Russia : Moscow
Country : Capital
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']Explanation:
Bonus: Here’s a quick look at how the Code: DictReader()Code: import csv population = []
with open('countries.csv', newline='', encoding='utf-8-sig') as csvfile: data = csv.DictReader(csvfile) for row in data: print(row)Output: Code: {'Country': 'Germany', 'Capital': 'Berlin', 'Population': '84,267,549', 'Area': '348,560'}
{'Country': 'France', 'Capital': 'Paris', 'Population': '65,534,239', 'Area': '547,557'}
{'Country': 'Spain', 'Capital': 'Madrid', 'Population': '46,787,468', 'Area': '498,800'}
{'Country': 'Italy', 'Capital': 'Rome', 'Population': '60,301,346', 'Area': '294,140'}
{'Country': 'India', 'Capital': 'Delhi', 'Population': '1,404,495,187', 'Area': '2,973,190'}
{'Country': 'USA', 'Capital': 'Washington', 'Population': '334,506,463', 'Area': '9,147,420'}
{'Country': 'China', 'Capital': 'Beijing', 'Population': '1,449,357,022', 'Area': '9,388,211'}
{'Country': 'Poland', 'Capital': 'Warsaw', 'Population': '37,771,789', 'Area': '306,230'}
{'Country': 'Russia', 'Capital': 'Moscow', 'Population': '146,047,418', 'Area': '16,376,870'}
{'Country': 'England', 'Capital': 'London', 'Population': '68,529,747', 'Area': '241,930'}It is evident from the output that Code: csv.DictReader()[b]Conclusion[/b] To sum things up, there are majorly four different ways of accessing specific columns from a given csv file:
Feel free to use the one that suits you best. I hope this tutorial helped you. Please subscribe and stay tuned for more interesting tutorials. Happy learning! Learn Pandas the Fun Way by Solving Code Puzzles If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link). ![]() It contains 74 hand-crafted Pandas puzzles including explanations. By solving each puzzle, you’ll get a score representing your skill level in Pandas. Can you become a Pandas Grandmaster? Coffee Break Pandas offers a fun-based approach to data science mastery—and a truly gamified learning experience. |