V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wliansheng
V2EX  ›  Python

怎么使用 pd,构建一对多的表格数据呢?

  •  
  •   wliansheng · 2022-12-05 17:54:49 +08:00 · 1512 次点击
    这是一个创建于 500 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问这种使用 DataFrame 该如何实现?

    图片注释
    5 条回复    2022-12-08 15:04:15 +08:00
    lookStupiToForce
        1
    lookStupiToForce  
       2022-12-05 18:56:28 +08:00   ❤️ 1
    two methods:

    1. multi level index
    intuitive but not recommended
    https://pandas.pydata.org/docs/user_guide/advanced.html

    2. duplicate values in first two columns to match third columns row count
    Alias4ck
        2
    Alias4ck  
       2022-12-06 01:59:45 +08:00   ❤️ 1
    ![test]( https://imgur.com/a/IdTSN4N) 不知道是不是你想要的 其实楼上也给出方案了
    wliansheng
        3
    wliansheng  
    OP
       2022-12-06 10:46:55 +08:00
    @lookStupiToForce
    @Alias4ck 十分感谢。
    craiiz
        4
    craiiz  
       2022-12-08 14:36:33 +08:00
    这个比较像透视表吧?直接 pd.pivot_table(df, index=["用户", "联系方式等信息"]) 应该就行了
    wliansheng
        5
    wliansheng  
    OP
       2022-12-08 15:04:15 +08:00
    @craiiz 感谢帮助,我去试试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1076 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:03 · PVG 03:03 · LAX 12:03 · JFK 15:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.