I've placed the contents of a .csv file in the data list with the following code in Jupyter notebook
data = []
with open("president_county_candidate.csv", "r") as f:
contents = csv.reader(f)
for c in contents:
data.append(c)
I can only get an element through the index number, but that gives me the whole row of the list. How can I choose specific elements and the count? In the image, you can see the content of the List(data).
CodePudding user response:
You can use pandas
library to read csv in form of Dataframes and also you can perform various operations on the same. Refer the below code:
import pandas as pd
df = pd.read_csv('president_county_candidate.csv')
print(df.shape)
This will give you the number of rows and columns present in CSV file.
Also in order to extract any specific column you can use:
newdf = df[['candidates', 'votes']]
This will give you the new dataframe with the above mentioned columns.
Also please find the solution for your approach mentioned in question
You can extract the column index and then while parsing the contents, mention the index number of the columns you need.
For example:
data = []
with open("president_county_candidate.csv", "r") as f:
contents = csv.reader(f)
for c in contents:
data.append([c[2], c[4]])
This will only get the data of Candidate and Votes.
Note: It's better to get the index number of specific column using list.index('columnName')
and pass the same variable instead of hard-coded index.