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 csv file (countries.csv):
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:
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
Explanation:
Read the csv file using pd.read_csv() Pandas function.
Save all the information of the columns Country and Capital within independent variables using
country = df['Country']
Alternatively, you can also use country = df.Country
capital = df['Capital']
Alternatively, you can also use capital = df.Capital
To display the country names and their capitals simultaneously, you can bind the two columns, country and capital, using the zip() function and then display each country along with its capital using a for loop upon the zipped object.
To display all the values in the population column, you can simply use df['Population'].
TRIVIA zip() is a built-in function in Python that takes an arbitrary number of iterables and binds them into a single iterable, a zip object. It combines the n-th value of each iterable argument into a tuple. Read more about zip() here.
➤List-Based Indexing of a DataFrame
In case you are not comfortable with using zip() 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:
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
Method 2: Integer Based Indexing with iloc
Approach: The idea here is to use the df.iloc[rows, columns].values 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.
rows is used to select individual rows. Use the slicing colon: to ensure all rows have been selected.
columns is used to select individual columns.
Use country = data.iloc[:, 0].values to save the values of the Country column.
capital = data.iloc[:, 1].values to save the values of the Capital column.
population = data.iloc[:, 2].values to save the values of the Population column.
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']
Method 3: Name-Based Indexing with loc()
Instead of selecting the columns by their index, you can also select them by their name using the df.loc[] selecter.
The following example shows how to select the columns Country and Capital from the given DataFrame.
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
csv 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:
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:
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 csv module and open up the csv file. Ensure that you feed in the encoding argument as it helps to eliminate any unreadable characters that may occur in the given csv file.
with open('countries.csv', newline='', encoding='utf-8-sig') as csvfile
Allow Python to read the csv file as a dictionary using csv.Dictreader object.
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 DictReader() class looks like:
import csv population = []
with open('countries.csv', newline='', encoding='utf-8-sig') as csvfile: data = csv.DictReader(csvfile) for row in data: print(row)
It is evident from the output that csv.DictReader() 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.
Conclusion
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 DictReader class.
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.
Oracle has published an update to the Java Client Roadmap which extends availability and support timelines for many Java Client related technologies. Executive Summary: Oracle has extended commercial support and updates for Java SE 8 from March 2025 to at least December 2030. Oracle has extended ind...
The games are free to keep until May 19th 2022 - 15:00 UTC.
Next week's freebie: mystery
We are welcoming everyone to join our discord[discord.gg]. We are more active there on finding giveaways, small or large, and there are daily raffles you can participate.
[www.indiegala.com] Hunt down a plethora of monsters with distinct behaviors and deadly ferocity. From classic returning monsters to all-new creatures inspired by Japanese folklore, including the flagship wyvern Magnamalo, you’ll need to think on your feet and master their unique tendencies if you hope to reap any of the rewards!
Lumote: The Mastermote Chronicles expands on the early access PC game, Lumote, supporting Luminawesome to deliver a complete version of the critically acclaimed release that delivers additional story, creates a whole new game world playing as the mighty Mastermote, and includes a new level of challenges, puzzles, and collectibles. With a brand-new gameplay trailer, narrated by Michelle Rocha, developer and co-founder of Luminawesome Games, players are taken on a journey through The Great Depths guided by the curious and adorable protagonist, Lumote. As the trailer dives into the underwater world, players are introduced to the bioluminescent creatures, known as the Motes, who spend their lives deep in the bioverse, content with living on the rhythms found in the electronica soundscape.
If you managed to get your hands on a Steam Deck, consider checking out the latest Humble Bundle--which offers eight Steam Deck-verified games for just $20. Included in the bundle you'll find Orcs Must Die 3,Neon Abyss,MechWarrior 5: Mercenaries, and more.
If you're not looking to drop $20, Humble is offering a $10 tier that includes Parkasaurus, Paint the Town Red, Exanima, and Orcs Must Die 2: Complete. Regardless of which bundle you pick, you'll be able to redeem all your games through Steam and benefit from full support for Steam Deck.
Orcs Must Die 3 is arguably the most well-known game of the bunch, offering over-the-top action that sees you laying traps and slaying legions of orcs as you protect your vulnerable rift. If solo play isn't chaotic enough for you, the game features a cooperative mode that lets you and a buddy team up for even more orc-slaying action.
Authored by: Georges Saab (VP, Java Platform Group and Chair, OpenJDK Governing Board) It is hard to believe that it has been 25 years since Java became officially available. On the other hand, it seems like several eras have passed - and in some sense, they have. I still remember the sense of excit...
Posted by: xSicKxBot - 05-11-2022, 09:46 AM - Forum: Python
- No Replies
Computer Science Researcher – Income & Opportunity
5/5 – (2 votes)
Before we learn about the money, let’s get this question out of the way:
What Does a Computer Science Research Scientist Do?
A computer science researcher and scientist identifies and answers open research questions in computer science. They apply scientific reasoning and research techniques to push the state-of-the-art forward in various fields such as machine learning, distributed systems, databases, algorithms, and data science.
These are some of the fields that are relevant for you as a computer science researcher:
Six of the most common daily activities of computer science researchers are:
reading research papers,
thinking about research questions and problems,
identifying research gaps and discussing them with their peers,
creating code and software systems for evaluation purposes,
writing research papers, and
presenting those scientific results at conferences and in journals.
How to Become a Computer Science Researcher?
The following section is based on my own experience and communications with hundreds of computer science researchers at various international conferences and various stages in their careers—from first year PhD students to prestigious computer science professors. My field of study is distributed systems.
A CS research scientist holds an advanced academic degree that’s formally required to work for a research institution such as a University. Thus, you should complete both the bachelor of science (BSc) and the master of science (MSc) in computer science or a related field to become a computer science researcher.
While the previous two steps, i.e., obtaining the BSc and MSc degrees, will be crucial on your path towards a computer science researcher, the following steps are a bit less strict.
First, it doesn’t harm to have good grades in your BSc and MSc degrees. However, you don’t need to have excellent grades for many reasons:
Income Potential in the Real World: Due to the high income potential of computer scientists — for example, as freelancers or highly-paid employees in Big Tech corporations — obtaining a research position in computer science is not as competitive as in other fields. When working as a PhD researcher myself a couple of years ago, I have seen many computer science researchers with average grades.
Variance of Quality Requirements in Universities: There are many top-notch Universities for computer science research such as Stanford or MIT with strong brands and outstanding researchers. It is very tough and potentially expensive to enter those Universities — even as a student. However, there are many 2nd and 3rd tier Universities that have much more demand for computer science researchers than supply. Getting a research position in a 2nd tier University may be as simple as applying for it after your successful completion of MSc in a computer science related field.
MSc Grades may not be available yet. When applying for a CS researching position, you often do so as a master of science student. This means that you don’t even know your final grade for the MSc degree which makes this grade irrelevant for your application as a CS researcher. Even if the grade may be available in your particular case, it is not for many other researchers so it’s not a must-have generally.
Second, any academic project (e.g., a published research paper) or even a writing project (e.g., having written a couple of online articles as a freelancer) can help you land the research position you desire. But again, that’s not a requirement and it will only increase your odds — all things being equal.
But don’t wait too long to acquire practical experience!
Even if you have little skills, it’s best to get started as a freelance developer and learn as you work on real projects for clients — earning income as you learn and gaining motivation through real-world feedback.
Tip: An excellent start to turbo-charge your freelancing career (earning more in less time) is our Finxter Freelancer Course. The goal of the course is to pay for itself!
Let’s have a look at the income potential of a computer science researcher next.
Annual Income
How much does a Computer Science Researcher make per year?
The median annual income (=50th percentile) of a computer science researcher was $131,490 in May 2021. The bottom 10% (=10th percentile) of computer science researchers earned less than $74,210 and the top 10% (=90th percentile) earned more than $208,000.
The source also lists the income for different industries as a computer science researcher:
Computer Science Researcher Industry
Annual Income
Private computer systems design and related services
$161,870
Corporate software publishers and software houses
$152,940
Research and development (R&D) in physical sciences, engineering, and life sciences
$132,810
Federal government
$112,310
Public and private universities, colleges, and professional schools
$79,510
Table: Income of computer science researchers by industry.
Learnings: You make the least amount of money when working as a researcher at University—even though it can open the door to lucrative six-figure opportunities in the private sector (e.g., working in the R&D team for BigTech corporations such as Google, Facebook, and Netflix).
Let’s have a look at the hourly rate of Computer Science Researchers next!
Hourly Rate
Computer Science Researchers are well-paid on freelancing platforms such as Upwork or Fiverr.
If you decide to go the route as a freelance Computer Science Researcher, you can expect to make between $30 and $150 per hour on Upwork (source). Assuming an annual workload of 2000 hours, you can expect to make between $60,000 and $300,000 per year.
This data is consistent with the official Bureau of Labor statistics that lists the median pay of a computer science researcher at $63 per hour (annualized income of $126,000).
Note: Do you want to create your own thriving coding business online? Feel free to check out our freelance developer course — the world’s #1 best-selling freelance developer course that specifically shows you how to succeed on Upwork and Fiverr!
The following statistic shows the self-reported income from 9,649 US-based professional developers (source).
The average annual income of professional developers in the US is between $70,000 and $177,500 for various programming languages.
Question: What is your current total compensation (salary, bonuses, and perks, before taxes and deductions)? Please enter a whole number in the box below, without any punctuation. If you are paid hourly, please estimate an equivalent weekly, monthly, or yearly salary. (source)
The following statistic compares the self-reported income from 46,693 professional programmers as conducted by StackOverflow.
The average annual income of professional developers worldwide (US and non-US) is between $33,000 and $95,000 for various programming languages.
Here’s a screenshot of a more detailed overview of each programming language considered in the report:
Here’s what different database professionals earn:
Here’s an overview of different cloud solutions experts:
Here’s what professionals in web frameworks earn:
There are many other interesting frameworks—that pay well!
Look at those tools:
Okay, but what do you need to do to get there? What are the skill requirements and qualifications to make you become a professional developer in the area you desire?
Let’s find out next!
General Qualifications of Professionals
StackOverflow performs an annual survey asking professionals, coders, developers, researchers, and engineers various questions about their background and job satisfaction on their website.
Interestingly, when aggregating the data of the developers’ educational background, a good three quarters have an academic background.
Here’s the question asked by StackOverflow (source):
Which of the following best describes the highest level of formal education that you’ve completed?
However, if you don’t have a formal degree, don’t fear! Many of the respondents with degrees don’t have a degree in their field—so it may not be of much value for their coding careers anyways.
Also, about one out of four don’t have a formal degree and still succeeds in their field! You certainly don’t need a degree if you’re committed to your own success!
Freelancing vs Employment Status
The percentage of freelance developers increases steadily. The fraction of freelance developers has already reached 11.21%!
This indicates that more and more work will be done in a more flexible work environment—and fewer and fewer companies and clients want to hire inflexible talent.
Here are the stats from the StackOverflow developer survey (source):
Do you want to become a professional freelance developer and earn some money on the side or as your primary source of income?
Resource: Check out our freelance developer course—it’s the best freelance developer course in the world with the highest student success rate in the industry!
Other Programming Languages Used by Professional Developers
The StackOverflow developer survey collected 58000 responses about the following question (source):
Which programming, scripting, and markup languages have you done extensive development work in over the past year, and which do you want to work in over the next year?
These are the languages you want to focus on when starting out as a coder:
And don’t worry—if you feel stuck or struggle with a nasty bug. We all go through it. Here’s what SO survey respondents and professional developers do when they’re stuck:
What do you do when you get stuck on a problem? Select all that apply. (source)
Related Tutorials
To get started with some of the fundamentals and industry concepts, feel free to check out these articles:
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
Temptation Saga Bundle, Dead Cells Deal CyberCry Sale
Temptation Saga Bundle | 10 Adult Steam Games | 89% OFF
[www.indiegala.com] Just when you thought it was over...the saga of temptation continues with a new alluring bundle filled with 10 attractive Adult 18+ Steam games.
Motion Twin & CyberCry Creators Sales, up to 70% OFF
[www.indiegala.com] [www.indiegala.com] Save an EXTRA 30% for every bundle & an extra 15% for every store purchase when paying with any supported crypto
Posted by: xSicKxBot - 05-11-2022, 09:46 AM - Forum: Lounge
- No Replies
CoD: Warzone Trailer Drops Hints For Upcoming Godzilla And King Kong LTM
A new trailer has arrived for Call of Duty: Warzone's upcoming Operation Monarch event on May 11. Godzilla and King Kong will be arriving to Caldera really soon, and while few details are known about the event, the trailer offers some clues on how this titan-sized LTM might work.
Operation Monarch's event is set to include a limited-time mode on Warzone's Caldera map, but Activision hasn't yet revealed how players will be interacting with the Monsterverse titans. However, the trailer does showcase the giant monoliths that can be found around the map. One set of monoliths has Kong's handprint on them, and the other set has Godzilla's name printed in Japanese Katakana. It's possible that the monoliths must be interacted with in order to summon a particular titan to an area.