如何将标量广播到pandas数据帧中的过滤列

时间:2013-03-06 13:57:45

标签: python pandas

我希望将表达式的结果广播到数据帧,但不是整个列,只是过滤子集。简化如下:

In [6]: df1 = DataFrame({"A":[1, 2, 3, 4], "B":["w", "x", "y", "z"], "C":(numpy.
zeros((4), dtype='S1'))})

In [7]: df1
Out[7]:
   A  B C
0  1  w
1  2  x
2  3  y
3  4  z

所以A和B包含我现有的数据,C列准备输入我的结果。所以我可以按如下方式广播到整个专栏:

In [9]: df1['C'] = 'H'

In [10]: df1
Out[10]:
   A  B  C
0  1  w  H
1  2  x  H
2  3  y  H
3  4  z  H

但是如果我尝试广播(在这个例子中,字母“R”)到过滤的子集:

In [14]: (df1[df1['A'] > 2])['C']
Out[14]:
2    H
3    H
Name: C

(只是为了证明过滤效果)

所以现在我尝试将“R”分配给这个子集..

In [12]: (df1[df1['A'] > 2])['C'] = "R"

In [13]: df1
Out[13]:
   A  B  C
0  1  w  H
1  2  x  H
2  3  y  H
3  4  z  H

但我的价值观保持不变:((虽然有趣的是我没有收到错误!?) 请问任何人都可以建议我能做到这一点吗?

非常感谢,

2 个答案:

答案 0 :(得分:5)

首先选择列,然后过滤:

df1['C'][df1['A'] > 2] = "R"

   A  B  C
0  1  w  H
1  2  x  H
2  3  y  R
3  4  z  R

答案 1 :(得分:1)

正如一句话:在这种情况下,大熊猫得到了很好的改进以发出警告:

In [8]: In [12]: (df1[df1['A'] > 2])['C'] = "R"
/Users/tismer/anaconda/bin/ipython:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  #!/bin/bash /Users/tismer/anaconda/bin/python.app

click to read the pandas link from above