How to access specific columns from a csv file?

0 votes
I have a csv file. I want to access only specific columns from it. Can it be done using csv module? Is there any other way to do it?
Jul 15, 2019 in Python by Arvind
• 2,980 points
7,345 views

1 answer to this question.

0 votes

Instead of using csv module in Python, I would suggest using the Pandas library. Refer the following code 

import pandas as pd
df1 = pd.read_csv(csv file) # read csv file and store it in a dataframe

Suppose if you want to extract columns A,B and C from your csv file then use the code in the following line

my_columns = df1[["A","B","C" ]]

print(my_columns) #this will print columns A,B,C
answered Jul 15, 2019 by Neel
• 3,010 points

Related Questions In Python

0 votes
2 answers
0 votes
1 answer

How to list a column of float values from csv file without importing (Python)

Using the default CSV module Demo: import csv with open(filename, "r") ...READ MORE

answered Sep 16 in Python by Rajiv
• 3,220 points
139 views
+1 vote
4 answers

How to write nested dictionaries to a CSV file

Using DictWriter there is no need in ...READ MORE

answered Oct 18, 2018 in Python by Richard William
13,987 views
0 votes
1 answer

How can I write nested dictionaries to a csv file?

You can use the pandas library to ...READ MORE

answered Apr 17, 2018 in Python by anonymous
388 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,460 views
0 votes
0 answers
0 votes
1 answer

How to write a python script for downloading a video from YouTube and saving it to a directory?

First download pytube using the following code  pip ...READ MORE

answered Sep 4, 2019 in Python by Neel
• 3,010 points
880 views
0 votes
1 answer

How to check if a given string matches a particular pattern in Python?

You can use re module of python ...READ MORE

answered Jul 3, 2019 in Python by Neel
• 3,010 points
197 views