0% found this document useful (0 votes)
2 views5 pages

Adding Columns in Pandas & Data Visualization

The document provides examples and explanations on various Python programming concepts, including adding new columns to a Pandas DataFrame, creating line graphs and bar charts using Matplotlib and Plotly, and understanding Python's garbage collection mechanism. It also covers iterating over keys and values in a dictionary. Key differences between Matplotlib and Plotly are mentioned, along with detailed examples for each topic.

Uploaded by

kavyalh054
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Adding Columns in Pandas & Data Visualization

The document provides examples and explanations on various Python programming concepts, including adding new columns to a Pandas DataFrame, creating line graphs and bar charts using Matplotlib and Plotly, and understanding Python's garbage collection mechanism. It also covers iterating over keys and values in a dictionary. Key differences between Matplotlib and Plotly are mentioned, along with detailed examples for each topic.

Uploaded by

kavyalh054
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

How to add new column in Pandas DataFrame


import pandas as pd
# Existing DataFrame
df = [Link]({'A': [1, 2, 3]})
df['B'] = [4, 5, 6] # First new column

# Add another new column


df['C'] = df['A'] + df['B'] # Column based on existing ones
print(df)

Output:
A B C
0 1 4 5
1 2 5 7
2 3 6 9
Here, column C is added by performing a calculation (A + B) on existing columns.
You can also add constant values or strings, e.g. df['D'] = 'Done'.

2. Write a python program to demonstrate how to create line graph


and bar chart using matplotlib and plotly. Explain the key
difference.

Mathplotlib- lab program

import [Link] as plt


# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 1. Plot a Line Graph
[Link](x, y, color='blue', marker='o', linestyle='--', label="Line Graph")
[Link]("Line Graph Example")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
[Link]()

# 2. Plot a Bar Chart


[Link](x, y, color='green', label="Bar Chart")
[Link]("Bar Chart Example")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
[Link]()

Using plotly
import [Link] as px
import pandas as pd
# Sample data
data = [Link]({'x': [1, 2, 3, 4, 5],
'y': [2, 4, 6, 8, 10]})
# Line graph
[Link](data, x='x', y='y', title='Line Graph Example').show()

# Bar chart
[Link](data, x='x', y='y', title='Bar Chart Example').show()
Key difference between matplotlib and plotly- (Refer PPT)

3. Write detailed explanation and example of how python's garbage collector


handles objects like lists and dictionaries for 5 marks

Garbage Collection in Python: Garbage collection in Python is the automatic process of


freeing memory that is no longer being used by the program.

When objects like lists, dictionaries, or other variables are no longer referenced, Python’s
garbage collector (GC) removes them from memory to make space for new objects.
How It Works?

Python mainly uses Reference Counting and a Cyclic Garbage Collector:

Reference Counting:

Each object keeps a count of how many references point to it.

When this count becomes zero, the object is automatically deleted.

Cyclic Garbage Collector:

If two or more objects refer to each other (a reference cycle), they won’t be deleted by
reference counting alone.

Python’s garbage collector periodically scans for such cycles and removes them.

Example

import gc

# Create a list and dictionary

my_list = [1, 2, 3]

my_dict = {'a': 10, 'b': 20}

# Both objects are in memory

print("Before deletion:", gc.get_count())

# Remove references

del my_list

del my_dict

# Run garbage collector manually

[Link]()

print("After garbage collection:", gc.get_count())

Explanation:

When my_list and my_dict are deleted, there are no references left.

The garbage collector detects this and frees their memory.


The function [Link]() forces garbage collection to occur immediately.

4. Iterating Over Keys and Values in a Dictionary

Definition: In Python, a dictionary stores data as key–value pairs.


To access all keys and values, we can iterate (loop) through the dictionary using a for
loop and the .items() method.

Syntax

for key, value in [Link]():

# use key and value

Example

# Create a dictionary

student = {'name': 'Cindy', 'age': 21, 'course': 'BCA'}

# Iterate over keys and values

for key, value in [Link]():

print(key, ":", value)

Output

name : Cindy

age : 21

course : BCA

Explanation

 .items() returns both keys and values together as pairs.


 The for loop assigns each key to key and its value to value in every iteration.
 This makes it easy to access and display both parts of each dictionary entry

You might also like