Pandas DataFrames
- What is a DataFrame?
A Pandas DataFrame is a 2 dimensional data structure, like a 2 dimensional array, or a table with rows and columns.
Example
You are tasked with creating a Pandas DataFrame to represent student information. The DataFrame should include columns for "StudentID," "Name," "Age," and "Grade." Create a Pandas DataFrame with the following data.
| StudentID | Name | Age | Grade |
|-----------|----------|-----|-------|
| 101 | Alice | 20 | A |
| 102 | Bob | 22 | B |
| 103 | Charlie | 21 | B |
| 104 | David | 23 | C |
| 105 | Emma | 20 | A |
Write Python code to create this DataFrame and display its contents.
import pandas as pd
data = {
"StudentID": [101, 102, 103, 104, 105],
"Name": ["Alice", "Bob", "Charlie", "David", "Emma"],
"Age": [20, 22, 21, 23, 20],
"Grade": ["A", "B", "B", "C", "A"]
}
df = pd.DataFrame(data)
print(df)
- Locate Row
Pandas use the loc attribute to return one or more specified row(s).
Example
Return row 0.
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print(df)
calories duration
0 420 50
1 380 40
2 390 45
- Named Indexes
With the index argument, you can name your own indexes.
Example
Add a list of names to give each row a name.
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
df = pd.DataFrame(data, index = ["day1", "day2", "day3"])
print(df)
calories duration
day1 420 50
day2 380 40
day3 390 45
- Locate Named Indexes
Use the named index in the loc attribute to return the specified rows.
Example
Return "berry".
print (df.loc["berry"])