Pandas 基本操作记录

案例1 数据行和列切换

python 复制代码
names = ["张三", "李四", "王五"]
subjets = ["语文", "数学"]

for idx, subjet in enumerate(subjets):
    tmp_data = {
        "张三": 1 * (10 ** idx),
        "李四": 2 * (10 ** idx),
        "王五": 3 * (10 ** idx),
        "赵六": 4 * (10 ** idx),
        "X": 5 * (10 ** idx),
    }
    print(subjet, tmp_data)

Ouput:

复制代码
语文 {'张三': 1, '李四': 2, '王五': 3, '赵六': 4, 'X': 5}
数学 {'张三': 10, '李四': 20, '王五': 30, '赵六': 40, 'X': 50}
  • 名字为Index
python 复制代码
df = pd.DataFrame()
for idx, subjet in enumerate(subjets):
    tmp_data = {
        "张三": 1 * (10 ** idx),
        "李四": 2 * (10 ** idx),
        "王五": 3 * (10 ** idx),
        "赵六": 4 * (10 ** idx),
        "X": 5 * (10 ** idx),
    }
    df[subjet] = pd.Series(tmp_data, index=names)
df
  • subject为Index
python 复制代码
data = []

for idx, subjet in enumerate(subjets):
    tmp_data = {
        "张三": 1 * (10 ** idx),
        "李四": 2 * (10 ** idx),
        "王五": 3 * (10 ** idx),
        "赵六": 4 * (10 ** idx),
        "X": 5 * (10 ** idx),
    }

    data.append(tmp_data)

new_df = pd.DataFrame(data, index=columns, columns=names)
new_df
相关推荐
2601_9669496518 小时前
Python 实战:如何统计五档盘口买卖盘总挂单量?附计算公式与代码
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
gCode Teacher 格码致知2 天前
Pandas教学25:NumPy 的实战价值集中的四件事
numpy·pandas
gCode Teacher 格码致知2 天前
Pandas教学24:NumPy、Pandas、Matplotlib三者之间的关系
numpy·pandas·matplotlib
2601_966949652 天前
为什么不应该把 QuantDash API Key 直接写进公开代码仓库?从 Git 提交历史理解凭证泄露风险
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
quantdash_cc3 天前
如何检查滚动收益率计算中的窗口长度不足?——从数学定义到 Pandas 实现
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
2601_966949653 天前
如何根据研究的起止时间截取分钟级行情?Python 实战与 QuantDash 数据处理指南
开发语言·python·pandas·量化交易·1024程序员节·股票数据·quantdash
benchmark_cc4 天前
如何为 QuantDash Python SDK 编写一个最小化的连接验证脚本?
python·数据分析·pandas·量化交易·股票数据·quantdash
万粉变现经纪人4 天前
如何解决 pip install 安装报错 检测到 yanked 版本需更换可用版本 问题
人工智能·python·scrapy·numpy·pandas·pyqt·pip
烂蜻蜓4 天前
Pandas入门教程(十三):Parquet 与 Feather——高性能列式存储格式实战
pandas
gCode Teacher 格码致知4 天前
Pandas教学-21:subplots的使用
python·pandas·1024程序员节