There are no items in your cart
Add More
Add More
Item Details | Price |
---|
Tue May 14, 2024
Pandas is a widely used Python library for data manipulation and analysis. It provides easy-to-use data structures and functions for working with structured data, making it a must-have tool for anyone dealing with data in Python. In this blog post, we'll explore how to import Pandas, discuss its key data structures, and provide some practical examples of how to use it.
import pandas as pd
Creating a DataFrame
Let's start by creating a simple DataFrame from scratch. We'll create a DataFrame that represents information about a few cities, including their names and populations. import pandas as pd
data = {
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],
'Population (millions)': [8.4, 3.9, 2.7, 2.3, 1.7] }
df = pd.DataFrame(data)
print(df)
The code above creates a DataFrame using a Python dictionary. Each key-value pair in the dictionary represents a column in the DataFrame. The resulting DataFrame will look like this:
Example 2: Reading Data from a CSV File
Pandas is incredibly useful for reading data from various file formats. Let's see how to read data from a CSV file and work with it. Suppose we have a CSV file named 'cities.csv' with city data:
import pandas as pd
# Read data from a CSV file
df = pd.read_csv('cities.csv')
# Display the first 5 rows of the DataFrame
print(df.head())
Vijay Kashyap
Python in simple and easy steps