You are on page 1of 9

Let’s create a simple DataFrame with some emoji data:

data = {'Emojis': ['🌞', '🌧️', '🌈', '🌼', '🐱'],

'Description': ['Sunshine', 'Rainy', 'Rainbow', 'Flower', 'Cat']}

df = pd.DataFrame(data)

print(df)using the `print` function:

print(df)

Output:

Emojis Description

0 🌞 Sunshine

1 🌧️Rainy

2 🌈 Rainbow

3 🌼 Flower

4 🐱 Cat

Data Selection and Slicing 🍽️


Pandas allows you to easily select and manipulate specific parts of
your data. For example, you can select a single column:

emojis = df[‘Emojis’]

print(emojis)

Output:

0🌞

1 🌧️

2🌈

3🌼

4🐱

Name: Emojis, dtype: object

Or slice rows based on a condition:

sunny_emojis = df[df[‘Description’] == ‘Sunshine’]

print(sunny_emojis)

Output:

Emojis Description

0 🌞 Sunshine
Data Analysis 📊

Pandas offers various functions for data analysis. Let’s calculate the
length of each emoji description:

df[‘Description Length’] = df[‘Description’].apply(len)

print(df)

Output:

Emojis Description Description Length

0 🌞 Sunshine 8

1 🌧️Rainy 5

2 🌈 Rainbow 7

3 🌼 Flower 6

4 🐱 Cat 3

Data Visualization 📈📉
Pandas also integrates well with data visualization libraries like
Matplotlib and Seaborn. Let’s visualize the description lengths:

import matplotlib.pyplot as plt

df.plot(kind=’bar’, x=’Emojis’, y=’Description Length’,


legend=False)
plt.title(‘Description Length of Emojis’)

plt.xlabel(‘Emojis’)

plt.ylabel(‘Description Length’)

plt.show()

This code creates a bar chart showing the length of emoji


descriptions.

Example 1: Creating a DataFrame


import pandas as pd

data = {‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’, ‘David’, ‘Eve’],

‘Age’: [25, 30, 35, 40, 45],

‘City’: [‘New York’, ‘Los Angeles’, ‘Chicago’, ‘Houston’,


‘Miami’]}

df = pd.DataFrame(data)

print(df)

Practice Questions:

1. Create a DataFrame named `fruits` with columns ‘Fruit’ and


‘Quantity’, containing data for ‘Apple’ (5), ‘Banana’ (8), ‘Orange’ (6),
and ‘Grapes’ (12).
Answers:

fruits_data = {‘Fruit’: [‘Apple’, ‘Banana’, ‘Orange’, ‘Grapes’],

‘Quantity’: [5, 8, 6, 12]}

fruits = pd.DataFrame(fruits_data)

print(fruits)

Example 2: Data Selection and Filtering


# Select the ‘Name’ column

names = df[‘Name’]

# Filter rows where age is greater than 30

above_30 = df[df[‘Age’] > 30]

print(names)

print(above_30)

Practice Questions:
2. From the `fruits` DataFrame, select only the rows where the
quantity is greater than or equal to 6.

3. Create a new DataFrame called `young_people` containing only


people from the `df` DataFrame who are 30 years old or younger.

Answers:
# Question 2

quantity_greater_than_6 = fruits[fruits[‘Quantity’] >= 6]

print(quantity_greater_than_6)

# Question 3

young_people = df[df[‘Age’] <= 30]

print(young_people)

Example 3: Data Analysis

# Calculate the average age


average_age = df[‘Age’].mean()

# Calculate the total quantity of fruits


total_quantity = fruits[‘Quantity’].sum()

print(f”Average Age: {average_age}”)

print(f”Total Quantity of Fruits: {total_quantity}”)

Practice Questions:
4. Calculate the maximum age of people in the `df` DataFrame.

5. Calculate the minimum quantity of fruits in the `fruits`


DataFrame.

Answers:

# Question 4
max_age = df[‘Age’].max()

print(f”Maximum Age: {max_age}”)

# Question 5
min_quantity = fruits[‘Quantity’].min()

print(f”Minimum Quantity of Fruits: {min_quantity}”)


Example 4: Data Manipulation
# Add a new column ‘Country’ to the DataFrame

df[‘Country’] = [‘USA’, ‘USA’, ‘USA’, ‘USA’, ‘USA’]

# Change the name of the ‘City’ column to ‘Location’

df.rename(columns={‘City’: ‘Location’}, inplace=True)

print(df)

Practice Questions:

6. Add a new column ‘Price’ to the `fruits` DataFrame with


prices $0.5, $0.25, $0.75, and $1.0 for ‘Apple’, ‘Banana’,
‘Orange’, and ‘Grapes’, respectively.

7. Rename the ‘Quantity’ column in the `fruits` DataFrame


to ‘Quantity in kg’.

Answers:

# Question 6

fruits[‘Price’] = [0.5, 0.25, 0.75, 1.0]


print(fruits)

# Question 7

fruits.rename(columns={‘Quantity’: ‘Quantity in kg’}, inplace=True)

print(fruits)

You might also like