0% found this document useful (0 votes)
5 views4 pages

Scenario Pandas

The document presents various scenarios using Python's Pandas library to manipulate DataFrames, including adding tax, converting temperatures, squaring numbers, formatting as currency, replacing zeros with NaN, converting strings to uppercase, rounding decimals, replacing negatives with zero, labeling numbers as even or odd, and masking values for privacy. It also explains the use of axis parameters for column-wise and row-wise operations in Pandas. A quick summary table is provided to clarify the direction of operations based on the axis specified.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Scenario Pandas

The document presents various scenarios using Python's Pandas library to manipulate DataFrames, including adding tax, converting temperatures, squaring numbers, formatting as currency, replacing zeros with NaN, converting strings to uppercase, rounding decimals, replacing negatives with zero, labeling numbers as even or odd, and masking values for privacy. It also explains the use of axis parameters for column-wise and row-wise operations in Pandas. A quick summary table is provided to clarify the direction of operations based on the axis specified.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

✅ Scenario 1: Add 5% Tax to Product Prices

python
import pandas as pd

df = pd.DataFrame({
'Product A': [100, 200],
'Product B': [150, 250]
})

Add 5% tax to each price


print(df.applymap(lambda x: round(x 1.05, 2)))

---

✅ Scenario 2: Convert Celsius to Fahrenheit

python
import pandas as pd

df = pd.DataFrame({
'City A': [0, 10, 20],
'City B': [5, 15, 25]
})

Celsius to Fahrenheit
print(df.applymap(lambda c: round((c 9/5) + 32)))

---

✅ Scenario 3: Square All Numbers in a Grid

python
import pandas as pd

df = pd.DataFrame({
'X': [2, 4],
'Y': [3, 5]
})

Square each number


print(df.applymap(lambda x: x 2))

---

✅ Scenario 4: Format Numbers as Currency

python
import pandas as pd

df = pd.DataFrame({
'Price 1': [1000, 2500],
'Price 2': [3200, 4500]
})
Format as currency string
print(df.applymap(lambda x: f"${x:,}"))

---

✅ Scenario 5: Replace Zeros with NaN

python
import pandas as pd
import numpy as np

df = pd.DataFrame({
'A': [1, 0, 3],
'B': [0, 5, 6]
})

Replace 0 with NaN


print(df.applymap(lambda x: np.nan if x == 0 else x))

---

✅ Scenario 6: Convert Strings to Uppercase

python
import pandas as pd

df = pd.DataFrame({
'Col1': ['apple', 'banana'],
'Col2': ['grape', 'orange']
})

Convert all strings to uppercase


print(df.applymap(str.upper))

---

✅ Scenario 7: Round Decimal Numbers to 1 Decimal Place

python
import pandas as pd

df = pd.DataFrame({
'A': [3.1415, 2.7182],
'B': [1.6180, 0.5772]
})

Round to 1 decimal place


print(df.applymap(lambda x: round(x, 1)))

---

✅ Scenario 8: Replace Negative Numbers with 0

python
import pandas as pd

df = pd.DataFrame({
'A': [5, -3, 7],
'B': [-1, 2, -6]
})

Replace negatives with 0


print(df.applymap(lambda x: x if x >= 0 else 0))

---

✅ Scenario 9: Convert Numbers to "Even"/"Odd" Labels

python
import pandas as pd

df = pd.DataFrame({
'Nums1': [1, 2, 3],
'Nums2': [4, 5, 6]
})

Label as Even or Odd


print(df.applymap(lambda x: 'Even' if x % 2 == 0 else 'Odd'))

---

✅ Scenario 10: Mask All Values with Stars for Privacy

python
import pandas as pd

df = pd.DataFrame({
'Passwords': ['abc123', 'myp@ss'],
'Keys': ['qwerty', 'zxcvb']
})

Mask characters with ''


print(df.applymap(lambda x: '' len(x)))

Note

In Pandas, axis=0 and axis=1 help you specify whether an operation should be done
by row or by column.

---

🔹 axis=0 → Column-wise operation

It means the operation is applied down each column.

➤ Think: "Move down the rows (operate column by column)"

✅ Example:

python
df.apply(sum, axis=0) Sum values in each column

---

🔹 axis=1 → Row-wise operation

It means the operation is applied across each row.

➤ Think: "Move across the columns (operate row by row)"

✅ Example:

python
df.apply(sum, axis=1) Sum values in each row

---

🔁 Quick Summary Table:

Axis Direction Operation is done on


---- ------------- --------------------
0 Top to Bottom Columns
1 Left to Right Rows

---

✅ Simple Scenario:

python
import pandas as pd

df = pd.DataFrame({
'A': [1, 2],
'B': [3, 4]
})

print(df.apply(sum, axis=0)) Output: A: 3, B: 7 (column-wise)


print(df.apply(sum, axis=1)) Output: 0: 4, 1: 6 (row-wise)

You might also like