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
相关推荐
linweidong2 天前
RTX 4090 Ubuntu + ComfyUI + comfy-kitchen 0.2.41 完整部署文档
机器学习·云原生·conda·pandas·scikit-learn
benchmark_cc3 天前
个人开发者接入五档盘口:用 QuantDash 读取单只股票的验收清单与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
benchmark_cc7 天前
用 QuantDash 读取单只股票五档盘口:抽样、缺失值检查与复现记录
python·数据分析·pandas·量化交易·股票数据·quantdash
benchmark_cc10 天前
单只股票五档盘口接入个人研究仓库:QuantDash 读取、校验与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
benchmark_cc11 天前
用 QuantDash 读取美股行情:AAPL.US 的运行前检查、排错顺序与数据校验清单
python·数据分析·pandas·量化交易·股票数据·quantdash
databook13 天前
经常听到的透视表是干什么的?
python·数据分析·pandas
benchmark_cc15 天前
A 股、港股、美股如何建立统一的标的主数据格式?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
benchmark_cc15 天前
用 00700.HK 做港股行情清洗:字段核对、校验与个人量化开发实践
python·数据分析·pandas·量化交易·股票数据·quantdash
第九序言T15 天前
数据分析岗 Pandas 面试题 20 道(上):概念与读代码
python·数据分析·pandas·数据处理
2601_9669496515 天前
如何封装 qd.klines.batch(),让批量 K 线查询函数在多个策略中复用?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash