gpt4 book ai didi

python - 如何为 Pandas 数据框的某些选定行共同设置多列的值?

转载 作者:行者123 更新时间:2023-11-28 22:53:33 24 4
gpt4 key购买 nike

<分区>

我有一个数据框 df,它有 'TPrice','THigh','TLow','TOpen','TClose','TPCLOSE' 列,和现在我想将 'TPrice','THigh','TLow','TOpen','TClose' 列值设置为与 'TPCLOSE' 列相同TPrice 列值为零的行。

显示一些 TPrice 为 0 的行:

>>> df[df['TPrice']==0][['TPrice','THigh','TLow','TOpen','TClose','TPCLOSE']][0:5]
TPrice THigh TLow TOpen TClose TPCLOSE
13 0 0 0 0 0 4.19
19 0 0 0 0 0 7.74
32 0 0 0 0 0 3.27
43 0 0 0 0 0 12.98
60 0 0 0 0 0 7.48

然后赋值:

>>> df[df['TPrice']==0][['TPrice','THigh','TLow','TOpen','TClose']] = df['TPCLOSE']

但 Pandas 并没有真正改变 df ,因为下面的代码仍然可以找到一些行:

>>> df[df['TPrice']==0][['TPrice','THigh','TLow','TOpen','TClose','TPCLOSE']][0:5]
TPrice THigh TLow TOpen TClose TPCLOSE
13 0 0 0 0 0 4.19
19 0 0 0 0 0 7.74
32 0 0 0 0 0 3.27
43 0 0 0 0 0 12.98
60 0 0 0 0 0 7.48

那怎么办呢?

Jeff 解决方案更新:

>>> quote_df = get_quote()
>>> quote_df[quote_df['TPrice']==0][['TPrice','THigh','TLow','TOpen','TClose','TPCLOSE','RT','TVol']][0:5]
TPrice THigh TLow TOpen TClose TPCLOSE RT TVol
13 0 0 0 0 0 4.19 -100 0
32 0 0 0 0 0 3.27 -100 0
43 0 0 0 0 0 12.98 -100 0
45 0 0 0 0 0 26.74 -100 0
60 0 0 0 0 0 7.48 -100 0
>>> row_selection = quote_df['TPrice']==0
>>> col_selection = ['THigh','TLow','TOpen','TClose']
>>> for col in col_selection:
... quote_df.loc[row_selection, col] = quote_df['TPCLOSE']
...
>>> quote_df[quote_df['TPrice']==0][['TPrice','THigh','TLow','TOpen','TClose','TPCLOSE','RT','TVol']][0:5]
TPrice THigh TLow TOpen TClose TPCLOSE RT TVol
13 0 4.19 4.19 4.19 4.19 4.19 -100 0
32 0 4.19 4.19 4.19 4.19 3.27 -100 0
43 0 4.19 4.19 4.19 4.19 12.98 -100 0
45 0 4.19 4.19 4.19 4.19 26.74 -100 0
60 0 4.19 4.19 4.19 4.19 7.48 -100 0
>>>

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