python - 根据 c 中的值乘以 pandas DataFrame 中的行

标签 python pandas database rows

我想从中得到:

<表类=“s-表”> <标题> 名称 电子邮件 电子邮件2 电子邮件3 电子邮件4 <正文> 斯坦 [email protected] 没有 [email protected] 没有 丹尼 [email protected] [email protected] [email protected] [email protected] Elle [email protected] 没有 没有 没有

对此:

<表类=“s-表”> <标题> 名称 电子邮件 <正文> 斯坦 [email protected] 斯坦 [email protected] 丹尼 [email protected] 丹尼 [email protected] 丹尼 [email protected] 丹尼 [email protected] Elle [email protected]

我知道我可以创建 4 个带有名称和电子邮件列的独立 DF,然后合并所有 4 个并用“否”删除那些,但我觉得可能有更智能、更动态的解决方案。

最佳答案

result = (
    df.set_index("nname")
    .stack()
    .to_frame("eemail")
    .query("eemail != 'NO'")
    .droplevel(1)
    .reset_index()
)

关于python - 根据 c 中的值乘以 pandas DataFrame 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71981935/

相关文章:

sql - 包含多个连接的 SQL 语句如何工作?

mysql - 使用 sequelizejs 进行自定义查询

python - 同时执行多个.py文件

python - 正确使用带生成器的线程池

python - 相隔 >292 年的日期差值

python - 在新的多索引级别下连接 Pandas 列

python - 如何通过 rpyc 获取命令的输出?

python - 从 itertools.combinations 中删除特定组合

Pandas:比较两个数据帧,如果列中不可用,则替换为特定值?

php - 从 html 页面提交时,值未插入数据库