2016-11-14 62 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