python pandas连续检查三个元素值,逻辑满足用另一个元素值替换所有三个元素值

我认为我在犯一个小错误,因此无法正常工作

我有这个数据框

open  high low close
0     10     0  0
1     1      1  1
0     12     0  0
0     13     0  0

我希望得到的输出是:

如果开盘价,低收盘价为零且高收盘价不为零,则用高值替换(开盘,低收盘价)。

另外一个选项

如果开,低,关为零且高也为零,请替换开并低至1的关口而高至10

尝试了1种逻辑,但仍未解决,什么是干净整洁的解决方案?

> opt_ce.loc[opt_ce['open'] == 0, 'open','low','close'] = opt_ce['high']
> opt_pe.loc[opt_pe['open'] == 0, 'open', 'low', 'close'] = opt_ce['high']
评论
回眸醉倾城
回眸醉倾城

你可以这样做:

df['open'] = df.apply(lambda x: x['high'] if x['open']==x['low']==x['close']==0 else x['open'], axis=1)
df['low'] = df.apply(lambda x: x['high'] if x['open']==x['low']==x['close']==0 else x['low'], axis=1)
df['close'] = df.apply(lambda x: x['high'] if x['open']==x['low']==x['close']==0 else x['close'], axis=1)
print(df)

   open  high  low  close
0    10    10   10     10
1     1     1    1      1
2    12    12   12     12
3    13    13   13     13
点赞
评论
珍VC
珍VC

尝试

check_columns = ['open', 'low', 'close']
condition = (df[check_columns].sum(axis=1) == 0) & (df['high'] > 0)

df.loc[condition, check_columns] = df['high']

输出量

   open  high  low  close
0    10    10   10     10
1     1     1    1      1
2    12    12   12     12
3    13    13   13     13
点赞
评论