-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,84 @@ | ||
# Python基础 | ||
# Pandas使用 | ||
|
||
```py | ||
pip install pandas | ||
``` | ||
|
||
|
||
## 数据读取 | ||
|
||
### 1. 读取表格 | ||
```py | ||
# -*- coding: utf-8 -*- | ||
df = pd.read_csv(encoding='utf-8', filepath_or_buffer='xxx') | ||
|
||
|
||
df = pd.read_excel('example.xlsx', usecols=['A', 'B']) | ||
``` | ||
|
||
### 2.获取表头 | ||
```py | ||
df.columns | ||
``` | ||
|
||
### 3.获取某一列去重后的值 | ||
```py | ||
df['column_name'].unique() | ||
``` | ||
|
||
## 数据操作 | ||
|
||
### 1.填充空数据 | ||
```py | ||
df['pageUrl'].fillna('') | ||
``` | ||
|
||
|
||
### 2.修改某列的所有数据 | ||
```py | ||
df['pageUrl'] = df['pageUrl'].map(lambda x: 'xxxx' + str(x)) | ||
``` | ||
|
||
### 3.新建表格 | ||
```py | ||
data = { | ||
'name': ['Alice', 'Bob', 'Charlie', 'David'], | ||
'age': [23, 30, 45, 25], | ||
'salary': [70000, 80000, 120000, 95000] | ||
} | ||
|
||
# 创建 DataFrame | ||
df = pd.DataFrame(data) | ||
``` | ||
|
||
|
||
|
||
## 数据筛选 | ||
|
||
### 1.筛选缺失的值 | ||
```py | ||
df['年龄'].isna() | ||
``` | ||
|
||
### 2.筛选出不包含特定值的行 | ||
```py | ||
df[~df['姓名'].isin(['张三'])] | ||
``` | ||
|
||
### 3.筛选日期 | ||
```py | ||
import datetime | ||
|
||
# 将 'timestamp' 列转换为日期时间格式 | ||
df['timestamp'] = df['timestamp'].apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')) | ||
|
||
# 提取 '年月日' 并去重 | ||
unique_days = df['timestamp'].dt.date.unique() | ||
``` | ||
|
||
## 数据计算 | ||
|
||
### 1.根据某种条件,对某一列求和 | ||
```py | ||
df[df['关联需求类型'] == '业务需求']['实际工时'].sum() | ||
``` |