3

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

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

但是我的价值观保持不变:((尽管有趣的是我没有收到错误!?)请任何人都可以提出一种我可以实现这一目标的方法吗?

非常感谢,

4

2 回答 2

6

首先选择列,然后过滤:

df1.loc[df1['A'] > 2, 'C'] = "R"

   A  B  C
0  1  w  H
1  2  x  H
2  3  y  R
3  4  z  R
于 2013-03-06T14:19:07.937 回答
1

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

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

点击阅读上面的熊猫链接

于 2015-05-07T10:46:01.793 回答