Python provides powerful libraries for data manipulation, and Pandas is one of the most widely used libraries for working with structured data. In this article, we will learn how to read a CSV file containing country data into a Pandas DataFrame.
If you haven't installed Pandas, you can do so using the following command:
pip install pandas
To load a CSV file into a Pandas DataFrame, use the read_csv function:
import pandas as pd
data = pd.read_csv("countries.csv")
print(data.head())
Here is an example of what a countries.csv file might look like:
Country,Capital,Population
India,New Delhi,1393409038
USA,Washington D.C.,331449281
UK,London,67886011
Canada,Ottawa,37742154
Australia,Canberra,25499884
Once loaded into a DataFrame, you can perform various operations such as filtering, sorting, and grouping. For example, to display countries with a population greater than 100 million:
filtered_data = data[data["Population"] > 100000000]
print(filtered_data)
Using Pandas, you can easily load, manipulate, and analyze country data from a CSV file. This is useful for data analysis, visualization, and other applications.