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

标签 python pandas

<分区>

我有一个数据框 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
>>> 

最佳答案

这个操作不会自动广播,所以你需要做这样的事情

In [17]: df = DataFrame(dict(A = [1,2,0,0,0],B=[0,0,0,10,11],C=[3,4,5,6,7]))

In [18]: df
Out[18]: 
   A   B  C
0  1   0  3
1  2   0  4
2  0   0  5
3  0  10  6
4  0  11  7

首先计算你想屏蔽哪些行(否则它们可能会随着你的进行而改变) 如果你正在修改 A(就像你在这里一样)

In [19]: mask = df['A'] == 0

In [20]: for col in ['A','B']:
   ....:     df.loc[mask,col] = df['C']
   ....:     

In [21]: df
Out[21]: 
   A  B  C
0  1  0  3
1  2  0  4
2  5  5  5
3  6  6  6
4  7  7  7

这需要进行更改以使其更自然(因为您正在将 rhs 上的系列分配给 lhs 上的数据框,而现在并没有像您认为的那样广播) https://github.com/pydata/pandas/issues/5206

关于python - 如何为 Pandas 数据框的某些选定行共同设置多列的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19346033/

相关文章:

python - 赋值之前引用的局部变量 'price_end'

python - 如何从该列中仅提取数字?

python - Pandas 时间序列数据 - 每 30 分钟计算过去 24 小时内的唯一值

python - 如何从数据框中的日期时间列中查找日期范围?

python - 使用 xlrd 读取 excel 时出现数字格式问题

Python - 如何在不使用 numpy 的情况下获取矩阵的上三角之和?

python - Opencv houghLines 不检测线

python - pandas - 如何根据第一次约会重新采样?

Pandas ,按计数分组并将计数添加到原始数据框?

python - 尝试从行尾获取值并将它们加在一起