推荐学习书目
› 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
songdg
V2EX  ›  Python

请教如何根据条件使用 numpy 函数生成一个新的数组

  •  
  •   songdg · Feb 15, 2023 · 2574 views
    This topic created in 1317 days ago, the information mentioned may be changed or developed.

    请教如何通过数组 A 、B 得到 C ,先判断 B 中是否有重复,然后在 A 中找最大的其余的设为 0 。 A = array([3.33, 1.18, 5.42, 4.12])、B = array([449, 3676, 10975, 10975])、C = array([3.33, 1.18, 5.42, 0])

    3 replies  •  2023-02-18 14:52:32 +08:00
    TimePPT
        1
    TimePPT  
       Feb 15, 2023   ❤️ 1
    import pandas as pd
    import numpy as np

    A = np.array([3.33, 1.18, 5.42, 4.12])
    B = np.array([449, 3676, 10975, 10975])
    df = pd.DataFrame(data={"A": A, "B": B})
    df["C"] = df.groupby("B")["A"].transform("max")
    df["C"] = df.apply(lambda row: 0 if row["C"] > row["A"] else row["C"], axis=1)
    df
    TimePPT
        2
    TimePPT  
       Feb 15, 2023
    随手写的,应该还有更简洁的写法
    songdg
        3
    songdg  
    OP
       Feb 18, 2023
    @TimePPT 谢谢帮助,我觉得还是写个函数来解决这个问题。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   861 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 20:16 · PVG 04:16 · LAX 13:16 · JFK 16:16
    ♥ Do have faith in what you're doing.