- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个数据框:
col1 col2
a 0
b 1
c 1
d 0
c 1
d 0
在 'col2'
上,我只想保留顶部的第一个 1
并将第一个下面的每个 1
替换为 0
,输出为:
col1 col2
a 0
b 1
c 0
d 0
c 0
d 0
非常感谢。
最佳答案
你可以找到第一个1
的索引,并将其他设置为0
:
mask = df['col2'].eq(1)
df.loc[mask & (df.index != mask.idxmax()), 'col2'] = 0
要获得更好的性能,请参阅 Efficiently return the index of the first value satisfying condition in array .
关于python - Pandas 数据框 : Remove secondary upcoming same value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53654729/
我正在使用此处提供的多合一事件日历:http://wordpress.org/extend/plugins/all-in-one-event-calendar/ 我正在尝试使用 WP_Query()
Stripe 文档对此含糊不清: https://stripe.com/docs/billing/webhooks A few days prior to renewal, your site rec
Stripe 文档对此含糊不清: https://stripe.com/docs/billing/webhooks A few days prior to renewal, your site rec
我想获得最近的星期一的日期(即不是过去的)。 因此,如果今天是星期二(2009 年 12 月 1 日),我想获取星期一(2009 年 12 月 7 日)的日期。 我如何用 Zend_Date 做到这一
我有一个表“users”,其中有一列“date_of_birth”(DATE 格式,包含日、月、年)。在前端,我需要列出 5 个即将到来的生日。 花了很长时间试图找出逻辑..也浏览了谷歌中所有可能的文
我有一个数据框: col1 col2 a 0 b 1 c 1 d 0 c 1 d 0 在 'col2' 上,我只想保留顶部的第一个 1 并
我们目前在移动应用程序中使用 PhoneGap,我知道他们有 device.udid (http://docs.phonegap.com/en/1.0.0/phonegap_device_device
我有一些旧的 Azure Powershell 脚本,我正在将它们拿出来再次使用。它们似乎按照我内存中的方式工作,除了我收到这些警告: $VirtualMachine = Add-AzureRmVMN
我是一名优秀的程序员,十分优秀!