一周学会Pandas2 Python数据处理与分析-编写Pandas2 HelloWord项目

锋哥原创的Pandas2 Python数据处理与分析 视频教程:

2025版 Pandas2 Python数据处理与分析 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili

我们首先准备一个excel文件,用来演示pandas操作数据集(数据的集合)。excel文件属于数据集的一种,其他的csv,网页,sql,剪贴板也算数据集。student_scores.xlsx文件。一共100条数据。包含姓名,学号,出生日期,性别,语文分数,数学分数,英语分数列信息。

当然这个excel数据集也是锋哥通过pandas生成的,先给下代码,等后面学到数据输出再细讲代码,暂时不要求掌握。( 需要额外安装openpyxl库 )

复制代码
import pandas as pd
import random
import string
from datetime import datetime, timedelta


def random_name():
    first_names = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王']
    last_names = [''.join(random.choices(string.ascii_letters, k=2))]
    return random.choice(first_names) + random.choice(last_names)


def random_date():
    start_date = datetime.strptime('2005-01-01', '%Y-%m-%d')
    end_date = datetime.strptime('2008-12-31', '%Y-%m-%d')
    random_days = random.randint(0, (end_date - start_date).days)
    return (start_date + timedelta(days=random_days)).strftime('%Y-%m-%d')


data = {
    '姓名': [random_name() for _ in range(100)],
    '学号': [f'S{random.randint(10000, 99999)}' for _ in range(100)],
    '出生日期': [random_date() for _ in range(100)],
    '性别': [random.choice(['男', '女']) for _ in range(100)],
    '语文分数': [random.randint(0, 100) for _ in range(100)],
    '数学分数': [random.randint(0, 100) for _ in range(100)],
    '英语分数': [random.randint(0, 100) for _ in range(100)]
}

df = pd.DataFrame(data)
df.to_excel('student_scores.xlsx', index=False)    

新建helloWorld.py

复制代码
# 导入pandas库,去别名pd
import pandas as pd

df = pd.read_excel('student_scores.xlsx')  # 读取excel,返回DataFrame数据集对象
print(df)

运行输出:

格式不太好看,我们可以学习下一节,用Jupyter,返回效果会好看很多。

相关推荐
weixin_4407305022 分钟前
函数return、参数、参数组、递归函数、高阶函数等小结
开发语言·python
天赐范式37 分钟前
天赐范式第181天:让视图开始切换——PBFT视图切换与从safety到liveness的跨越
python·pbft·数字生命·天赐范式·动态运行时·拜占庭容错
sugarzhangnotes1 小时前
【无标题】
开发语言·人工智能·python
weixin_447195291 小时前
【无标题】
pytorch·python
迅猛龙办公室1 小时前
Python实现简单的人名对话
python
阿坨2 小时前
firestart:一行命令启动你的日常应用和网页
python·pypi·cli·click
老歌老听老掉牙2 小时前
麻花钻切屑形态演变的力学机制与临界条件分析
python·算法·钻头
happylifetree3 小时前
Python18(补充):练习
python
weixin_416667963 小时前
银河麒麟V10看门狗试验-1
网络·chrome·python
古城小栈4 小时前
Pydantic 从入门到实践全讲解
python