05-12-2022, 12:52 PM
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 file ():
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:
Output:
Explanation:
TRIVIA
is a built-in function in Python that takes an arbitrary number of iterables and binds them into a single iterable, a object. It combines the value of each iterable argument into a tuple.
Read more about zip() here.
[b]➤ List-Based Indexing of a DataFrame[/b]
In case you are not comfortable with using to display multiple columns at once, you have another option. You can simply use list-based indexing to accomplish your goal.
List-based indexing is a technique that allows you to pass multiple column names as a list within the square-bracket selector.
Example:
Output:
[b]Method 2: Integer Based Indexing with iloc[/b]
Approach: The idea here is to use the 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.
Output:
[b]Method 3: Name-Based Indexing with loc()[/b]
Instead of selecting the columns by their index, you can also select them by their name using the selecter.
The following example shows how to select the columns and from the given DataFrame.
Output:
Related Tutorial: Slicing Data from a Pandas DataFrame using .loc and .iloc
[b]Method 4: Using csv Module[/b]
module is yet another spectacular option in Python that allows you to play with csv files. Let us have a look at the code that helps us to read the given csv file and then read specific columns from it:
Output:
Explanation:
Bonus: Here’s a quick look at how the class looks like:
Output:
It is evident from the output that returns a dictionary for each row such that the column header is the key while the value in the row is the associated value in the dictionary.
[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).
![[Image: cover.jpg]](https://blog.finxter.com/wp-content/uploads/2020/11/cover.jpg)
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.
Rate this post
Table of Contents
- Method 1: Using Pandas
- Method 2: Integer Based Indexing with iloc
- Method 3: Name-Based Indexing with loc()
- Method 4: Using csv Module
- Conclusion
- Learn Pandas the Fun Way by Solving Code Puzzles
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
- column along with theCode:
Countrycolumn?Code:Capital
- All values in the column?Code:
population
[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:
- Read the csv file using Pandas function.Code:
pd.read_csv()
- Save all the information of the columns Country and Capital within independent variables using
- Code:
country = df['Country']- Alternatively, you can also use Code:
country = df.Country
- Alternatively, you can also use
- Code:
capital = df['Capital']- Alternatively, you can also use Code:
capital = df.Capital
- Alternatively, you can also use
- To display the country names and their capitals simultaneously, you can bind the two columns, andCode:
country, using the zip() function and then display each country along with its capital using a for loop upon the zipped object.Code:capital
- To display all the values in the population column, you can simply use .Code:
df['Population']
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- is used to select individual rows. Use the slicing colonCode:
rowsto ensure all rows have been selected.Code::
- is used to select individual columns.Code:
columns- Use to save the values of the Country column.Code:
country = data.iloc[:, 0].values
- to save the values of the Capital column.Code:
capital = data.iloc[:, 1].values
- to save the values of the Population column.Code:
population = data.iloc[:, 2].values
- Use
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:
- Import the module and open up the csv file. Ensure that you feed in theCode:
csvargument as it helps to eliminate any unreadable characters that may occur in the given csv file.Code:encoding- Code:
with open('countries.csv', newline='', encoding='utf-8-sig') as csvfile
- Allow Python to read the csv file as a dictionary using object.Code:
csv.Dictreader
- Once the file has been read in the form of a dictionary, you can easily fetch the values from respective columns by using the keys within square bracket notation from the dictionary. Here each column represents the key within the given dictionary.
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:
- List-Based Indexing.
- Integer-Based Indexing.
- Name-Based Indexing.
- Using csv modules class.Code:
DictReader
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).
![[Image: cover.jpg]](https://blog.finxter.com/wp-content/uploads/2020/11/cover.jpg)
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.

