pandas data frame
pandas data frame
1 Pandas DataFrame**
**What is it?**
A DataFrame is a table with rows and columns, like a spreadsheet, where you can store and manage data.
**Real-life Example:**
Your grocery list is a table with items, quantities, and prices.
**Python Example:**
```
**Output:**
---
**Real-life Example:**
You write your grocery list in a notebook, and now you want to put it into a program.
**Python Example:**
# Creating from a dictionary
data = {"Item": ["Eggs", "Cheese"], "Quantity": [12, 1], "Price": [4, 5]}
df = pd.DataFrame(data)
print(df)
```
**Output:**
```
**Real-life Example:**
Your friend emails you a CSV file with their grocery list, and you load it into Python.
**Python Example:**
If you’ve made a CSV file in Notepad or any other app, I’ll show you how to bring it into Python step-by-step
with an easy explanation and example.
Real-Life Example
Imagine you wrote a shopping list in Notepad:
Item,Price
Cake,5
Juice,2
**Real-life Example:**
You want to know how many items and details are in your grocery list.
**Python Example:**
```python
import pandas as pd
data = {"Item": ["Apples", "Bread"], "Quantity": [5, 2], "Price": [1, 2]}
df = pd.DataFrame(data)
print("Dimensions:", df.shape) # (rows, columns)
```
**Output:**
```
Dimensions: (2, 3)
```
(2 rows for Apples and Bread, 3 columns for Item, Quantity, Price)
---
**Python Example:**
```python
import pandas as pd
data = {"Item": ["Apples", "Bread"], "Quantity": [5, 2], "Price": [1, None]}
df = pd.DataFrame(data)
print(df.info())
```
**Output:**
```
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Item 2 non-null object
1 Quantity 2 non-null int64
2 Price 1 non-null float64
dtypes: float64(1), int64(1), object(1)
memory usage: 176.0+ bytes
```
(Notice Price has 1 missing value!)
---
**Real-life Example:**
You want only the “Quantity” column or just the row for “Bread.”
**Python Example:**
```python
import pandas as pd
data = {"Item": ["Apples", "Bread", "Milk"], "Quantity": [5, 2, 1], "Price": [1, 2, 3]}
df = pd.DataFrame(data)
Bread row:
Item Bread
Quantity 2
Price 2
Name: 1, dtype: object
```
---
**Real-life Example:**
You calculate the average price of your groceries or the total quantity.
**Python Example:**
```python
import pandas as pd
data = {"Item": ["Apples", "Bread", "Milk"], "Quantity": [5, 2, 1], "Price": [1, 2, 3]}
df = pd.DataFrame(data)
---
**Real-life Example:**
You decide to buy 10 apples instead of 5 and add “Eggs” to your list.
**Python Example:**
```python
import pandas as pd
data = {"Item": ["Apples", "Bread"], "Quantity": [5, 2], "Price": [1, 2]}
df = pd.DataFrame(data)
print(df)
```
**Output:**
```
Item Quantity Price
0 Apples 10 1
1 Bread 2 2
2 Eggs 12 4
```
---
**Real-life Example:**
After updating your grocery list, you save it to share with your friend.
**Python Example:**
```python
import pandas as pd
data = {"Item": ["Apples", "Bread"], "Quantity": [5, 2], "Price": [1, 2]}
df = pd.DataFrame(data)
---
**Real-life Example:**
You track groceries by week and want the total cost per week.
**Python Example:**
```python
import pandas as pd
data = {
"Week": ["Week1", "Week1", "Week2", "Week2"],
"Item": ["Apples", "Bread", "Milk", "Apples"],
"Price": [1, 2, 3, 1]
}
df = pd.DataFrame(data)
---
Let me know if you’d like me to tweak any part or add more examples!