Issue
I want to be able to print out the name of the continent that has the most capital cities from a CSV data frame. Note: I'm doing this in JupyterLab.
This is what the question asks: Print out the continent with the most capitals.
Here is what I did:
import csv
with open(`C:/Users/user/AppData/Local/Programs/Python/Python/Scripts/caps.csv', "r") as continent:
reader = csv.reader(continent, delimiter='\t')
for lines in reader:
print(lines[5])
However, the above code prints every continent in column 5. How would you print out just one continent e.g. "Africa" from the column?
Solution
You may use as well the pandas
package, which is very useful to load and manage csv files. Then with following code you can get all entries from Africa.
import pandas as pd
df = pd.read_csv(r"C:/Users/user/AppData/Local/Programs/Python/Python/Scripts/caps.csv")
dfAfrica = df[df[df.columns[5]]=="Africa"]
Then the variable dfAfrica
would be the DataFrame
you can print and see the data you were willing to filter.
Answered By - Cedric Zoppolo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.