gpt4 book ai didi

python - 如何获取 X 列为 False 的同一条船上连续安装的最旧部件安装?

转载 作者:行者123 更新时间:2023-12-01 07:10:03 26 4
gpt4 key购买 nike

我有一个很好的算法,可以返回同一条船上连续安装的最旧的部分安装:How to get oldest part installation of consecutive installations the same boat in pandas?

我只想将此算法应用于具有 col['x'] == False 的行。我怎样才能做到这一点?请参阅下面的预期输出。

算法:print (df[['boat','part']].ne(df[['boat','part']].shift()).any(axis=1))

输入

| boat | part | date           | x     |
|------|------|----------------|-------|
| A | B | 12/19/13 08:19 | TRUE |<-- ignore and keep this
| A | C | 3/24/14 10:26 | TRUE |<-- ignore and keep this
| A | D | 7/21/14 09:46 | TRUE |<-- ignore and keep this
| A | D | 3/5/18 22:35 | FALSE |<--evaluate this with algorithm
| A | D | 7/27/19 00:43 | FALSE |<--evaluate this with algorithm
| A | D | 8/9/19 00:53 | FALSE |<--evaluate this with algorithm
| A | D | 9/6/19 23:10 | FALSE |<--evaluate this with algorithm

预期输出

| boat | part | date           | x     |
|------|------|----------------|-------|
| A | B | 12/19/13 08:19 | TRUE |<-- keep because x == True
| A | C | 3/24/14 10:26 | TRUE |<-- keep because x == True
| A | D | 7/21/14 09:46 | TRUE |<-- keep because x == True
| A | D | 3/5/18 22:35 | FALSE |<-- Keep because first install of consecutive installs

输入2

| boat | part | date             | x     |
|------|------|------------------|-------|
| A | E | 12/20/13 03:27 | TRUE |<-- ignore and keep this
| B | E | 7/21/14 09:46 | FALSE |<--evaluate this with algorithm
| C | E | 1/8/16 17:08 | TRUE |<-- ignore and keep this
| C | E | 3/23/16 17:15 | TRUE |<-- ignore and keep this
| B | E | 3/5/18 22:35 | FALSE |<--evaluate this with algorithm
| B | E | 7/27/19 00:43 | FALSE |<--evaluate this with algorithm
| B | E | 8/9/19 00:53 | FALSE |<--evaluate this with algorithm
| B | E | 9/6/19 23:10 | FALSE |<--evaluate this with algorithm

预期输出 2

| boat | part | date             | x     |
|------|------|------------------|-------|
| A | E | 12/20/13 03:27 | TRUE |<-- keep because x == True
| B | E | 7/21/14 09:46 | FALSE |<-- Keep because first install of consecutive installs
| C | E | 1/8/16 17:08 | TRUE |<-- keep because x == True
| C | E | 3/23/16 17:15 | TRUE |<-- keep because x == True
| B | E | 3/5/18 22:35 | FALSE |<-- Keep because first install of consecutive installs

最佳答案

一个想法是,如果 DataFrame.assign True 为 true,则创建由唯一值填充的新辅助列和 Index.where并处理所有 3 列:

df1 = df.assign(tmp = df.index.where(df['x'], -1))[['boat','part','tmp']]
df = df[df1.ne(df1.shift()).any(axis=1)]
print (df)
boat part date x
0 A E 12/20/13 03:27 True
1 C E 1/8/16 17:08 True
2 C E 3/23/16 17:15 True
3 B E 3/5/18 22:35 False

第二个:

df1 = df.assign(tmp = df.index.where(df['x'], -1))[['boat','part','tmp']]
df = df[df1.ne(df1.shift()).any(axis=1)]
print (df)
boat part date x
0 A E 12/20/13 03:27 True
1 B E 7/21/14 09:46 False
2 C E 1/8/16 17:08 True
3 C E 3/23/16 17:15 True
4 B E 3/5/18 22:35 False

关于python - 如何获取 X 列为 False 的同一条船上连续安装的最旧部件安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58266534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com