CSV to Pandas (JavaScript)







Advertisement




CSV to Pandas

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.

Installing Pandas

If you haven't installed Pandas, you can do so using the following command:

pip install pandas

Loading a CSV File

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())
    

Sample CSV File

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
    

Working with the Data

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)
    

Conclusion

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.