2016-11-14 67 views
0

我试图将过滤的数据框传递到一个函数,该函数是为了操纵一些列(再次使用过滤)。我知道这已经出现了很多次,但即使在阅读了文档和其他相关问题之后,我仍然遇到困难。我想我只需要一个可以从中开始尝试的实例。熊猫:更改过滤的数据帧列

这是我失败的尝试。 s1应该是从我的实际用例传递到列操作函数的值。

>>> import pandas as pd 
>>> 
>>> df1 = pd.DataFrame({'a': [1, 2, 3, 4], 'b': [ 8, 7, 6, 5]}) 
>>> df1 
    a b 
0 1 8 
1 2 7 
2 3 6 
3 4 5 
>>> 
>>> s1 = df1.loc[df1['a']<=2, :] 
>>> s1 
    a b 
0 1 8 
1 2 7 
>>> s1['b'] = 0 
__main__: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 caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 
>>> df1 
    a b 
0 1 8 
1 2 7 
2 3 6 
3 4 5 
>>> s1.loc[:, 'b'] = 0 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/indexing.py:508: 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 caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 
    self.obj[item_labels[indexer[info_axis]]] = value 
>>> df1 
    a b 
0 1 8 
1 2 7 
2 3 6 
3 4 5 
>>> s2 = df1[df1['a']<=2] # next try: # this seems to create a detached copy of df1 
>>> s2 
    a b 
0 1 8 
1 2 7 
>>> s2.loc[:,'b']=0 
>>> df1 # df1 didn't change :-(
    a b 
0 1 8 
1 2 7 
2 3 6 
3 4 5 
>>> s2 # ... only filtered copy of df1 did. 
    a b 
0 1 0 
1 2 0 
+0

你试图达到什么目的? – James

+0

我正在使用函数f(df)来修改数据帧的某些列。如果可能的话,我希望透明地传入过滤的数据框,以便只修改行的子集(实际上,此功能会执行一些额外的过滤并仅修改子集的子集 - 不确定是否相关)。 – orange

+0

我其实认为这是不可能的。你不能创建一个数据框的子集,改变它的数据,并期望原始数据框被改变,除非你用'ix','loc'等在一行中完成所有操作。任何人都可以证实这一点吗? – orange

回答

0

我认为你应该使用功能.IX

例如:

df1.ix[df1['a']<=2, 'b'] = 0 

是要达到什么目的?

+0

没有,也不起作用。我的用例的等价物将是s3 = df1.ix [df1 ['a'] <= 2,:]'然后是's3 ['b'] = 4'(在一个函数中不知道“关于过滤器的任何事情)不会改变'df1'。 – orange

+0

你想要做的是对于b有0,其中a <= 2,对吗? – angelwally