일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- Text Analytics
- train/test
- matplotlib
- PROJECT
- line width
- variables
- __init__
- Default X points
- error
- iterates
- For loops
- self parameter
- pie charts
- matplotlib.pyplot
- multiple lines
- start exercise
- Else
- PANDAS
- machine learning
- polynomial regression
- continue
- SQL
- break
- data distribution
- Python
- line color
- Text mining
- AS
- Github
- MySQL
- Today
- Total
Data Science Explorer
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"])
'Python' 카테고리의 다른 글
Pandas: Cleaning Empty Cells (2) | 2023.10.29 |
---|---|
Pandas Read CSV (0) | 2023.10.28 |
Pandas Series (0) | 2023.10.26 |
Pandas (0) | 2023.10.25 |
[Basic Grammar] Python Strings: Modify Strings (0) | 2023.10.22 |