Pandas 1

Pandas Lesson 1 – Introduction, Series & DataFrames

Pandas Lesson 1 – Introduction, Series & DataFrames

🔹 Introduction

Pandas is one of the most powerful libraries in Python for data analysis. It helps us work with tables, spreadsheets, and data files in an easy way.

In this lesson, we will learn about:

  • Installing and importing Pandas
  • Series – like a column of data
  • DataFrame – like a table of rows and columns

🔹 Installing Pandas

pip install pandas
  

🔹 Importing Pandas

import pandas as pd
  

🔹 Pandas Series

A Series is like a single column of data, with an index.

import pandas as pd

data = [100, 200, 300, 400]
shares = pd.Series(data, index=["Reliance", "TCS", "Infosys", "Wipro"])
print(shares)
    

Output:

Reliance    100
TCS         200
Infosys     300
Wipro       400
dtype: int64
    

🔹 Pandas DataFrame

A DataFrame is like a table of rows and columns.

import pandas as pd

data = {
    "Company": ["Reliance", "TCS", "Infosys"],
    "Price": [2450, 3600, 1520],
    "Change": [12, -5, 8]
}

df = pd.DataFrame(data)
print(df)
    

Output:

   Company   Price  Change
0  Reliance   2450      12
1       TCS   3600      -5
2   Infosys   1520       8
    

🔹 Accessing Data

print(df["Company"])      # Column
print(df.loc[0])          # Row by index
print(df.iloc[1,1])       # Row 1, Column 1
    

📝 Practice Problems

Q1. Create a Series of prices of 4 fruits – Apple, Mango, Banana, Orange.

import pandas as pd
fruits = pd.Series([100, 60, 40, 80], index=["Apple","Mango","Banana","Orange"])
print(fruits)
    

Q2. Make a DataFrame of 3 students with Name, Age, and Marks.

data = {
  "Name": ["Amit", "Ravi", "Priya"],
  "Age": [20, 21, 19],
  "Marks": [85, 90, 88]
}
df = pd.DataFrame(data)
print(df)
    

Q3. Access the marks of the second student from the DataFrame above.

print(df.iloc[1,2])
    

Post a Comment

0 Comments

Me