惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 三叶草╮

DeepAgents 长期记忆相关组件详解 DeepAgents 长期记忆 笔记 Python 机器学习03 - 常见分类算法 Python 机器学习02 - 常见分类算法 Python uv 包管理 Python机器学习01 - Sklearn Python高级编程笔记 (线程/进程/协程) Python高级编程笔记 Python 基础笔记 Python Pandas Python playwright 笔记 pipreqs:快速准确生成当前项目的requirements.txt,还有和freeze的对比 WPF 4款 UI 库 C# Selenium [转]在WPF中自定义控件 UserControl [转]WPF的依赖属性是怎么节约内存的 [转]WPF中的导航框架 [转]C#对Excel报表进行操作(读写和基本操作) C# 模拟http请求网页数据 [网页爬虫]
Pandas 常用操作 (缺失值处理/排序/字符串处理/Index/Merge/合并)
三叶草╮ · 2025-03-04 · via 博客园 - 三叶草╮

处理示例:

        清洗成 ->     

Code:

import pandas as pd

# 读取Excel,跳过前面两行空行
studf = pd.read_excel(r'student_excel.xlsx', skiprows=2)
print(studf)
# 删除掉全部是空值的列
studf.dropna(axis='columns', how='all', inplace=True)
# 删除掉全部是空值的行
studf.dropna(axis='index', how='all', inplace=True)
# 将分数列为空的单元格填充为0
studf = studf.fillna({'分数': 0})
# 将姓名的缺失值进行前向填充
studf.loc[:, '姓名'] = studf['姓名'].ffill()
print(studf)
# 保存到新的Excel中 不保存index列
studf.to_excel(r'student_excel_clean.xlsx', index=False)

df.sort_values(by=['aqiLevel', 'bWendu'], ascending=[True, False], inplace=True)

# 将eg 2025-02-01 改为 提取到月份,且不要横线 如202502
df['date'] = df['date'].str.replace('-', '').str.slice(0, 6)
# 使用正则表达式处理 eg 将2025年01月02日中的年月日去掉,得到20250102
df['date'] = df['date'].str.replace(r'[年月日]', '', regex=True)

 

          

Pandas Index :

import timeit
import pandas as pd

file = r'ratings.csv'
df = pd.read_csv(file)
# drop=False,让索引列保留在数据集中
df.set_index('userId', inplace=True, drop=False)
# 使用索引查询userId=500的前5个行   效率更高
print(df.loc[500].head(5))
# 使用数据列的userId=500查询前5个行
print(df.loc[df['userId'] == 500].head(5))
# 判断索引是否单调递增
print(df.index.is_monotonic_increasing)
# 判断索引是否唯一
print(df.index.is_unique)

def my_function():
    # 这里放置你要测试的代码
    return df.loc[df['userId'] == 500].head(5)

# 使用 timeit 测试函数的执行时间
execution_time = timeit.timeit(my_function, number=1000)
print(f"执行时间: {execution_time} 秒")

# 使用Index实现数据集的自动对齐
s1 = pd.Series([1, 2, 3], index=list('abc'))
s2 = pd.Series([4, 5, 6], index=list('bcd'))
print(s1 + s2)
# 使用 add 方法并设置 fill_value 参数
result = s1.add(s2, fill_value=0)
print(result)

 Pandas Merge:

# 默认按行连接
result = pd.concat([df1, df2])
# 其余参数 axis 按行或按列对其,join='inner' 按交集连接,join='outer' 按并集连接,ignore_index=True 重新编号
result = pd.concat([df1, df2], axis=0, join='inner', ignore_index=True)
df3 = df1._append(df2)

Pandas Group By