Python 如何修改数据帧?

Python 如何修改数据帧?,python,pandas,Python,Pandas,这是我的简单数据框,我尝试使用 date_part spot datetimes bad_ticks gap_count bet_duration wins trend std trend_old fakei 1357113602 1357113602 344.25 2013-01-02 1 -9999 60 1 0 -9999 -9999 0 1357113603 1357113602 344.25 20

这是我的简单数据框,我尝试使用

date_part   spot    datetimes   bad_ticks   gap_count   bet_duration    wins    trend       std trend_old   fakei
1357113602  1357113602  344.25  2013-01-02  1   -9999   60  1   0   -9999   -9999   0
1357113603  1357113602  344.25  2013-01-02  0   -9999   60  1   0   -9999   -9999   1
1357113604  1357113604  348.53  2013-01-02  1   -9999   60  1   0   -9999   -9999   2
这不管用。我查了韦斯·麦肯锡的书,我的方法似乎是正确的。不知道为什么它不起作用。奇怪的是,如果我打字

und_data['trend'][und_data[und_data['fakei']==0].index]
1357113602    2
und_data['trend'][und_data[und_data['fakei']==0].index]=3
它给出的输出为3…但在下面的输出中没有显示。有些东西太容易与副本混淆?有人能凭直觉解释这一点吗。这些简单的事情在熊猫身上似乎很复杂

und_data['trend'][und_data[und_data['fakei']==0].index]

我不完全确定是否正确复制了您的数据,但我认为您需要:

und_data.head()

date_part   spot    datetimes   bad_ticks   gap_count   bet_duration    wins    trend       std trend_old   fakei
1357113602  1357113602  344.25  2013-01-02  1   -9999   60  1   0   -9999   -9999   0
1357113603  1357113602  344.25  2013-01-02  0   -9999   60  1   0   -9999   -9999   1
1357113604  1357113604  348.53  2013-01-02  1   -9999   60  1   0   -9999   -9999   2

谢谢,成功了!这仍然是奇怪的语法。为什么这样不行?und_data.loc[und_data[und_data['fakei']=0]。index]['trend']=1。没有合乎逻辑的解释。熊猫中有趣的东西???@coffeequant因为这就是所谓的链式分配,可能有效,也可能无效,所以您应该使用
.loc
.iloc
.ix
如果要分配给索引对象,请参阅
und_data.loc[und_data['fakei'] == 0, 'trend'] = 3

und_data.head()
Out[10]: 
                       date_part        spot  datetimes  bad_ticks  gap_count  1357113602 1357113602     344.25  2013-01-02          1      -9999         60   
1357113603 1357113602     344.25  2013-01-02          0      -9999         60   
1357113604 1357113604     348.53  2013-01-02          1      -9999         60   

                       bet_duration  wins  trend  std trend_old  fakei  
1357113602 1357113602             1     0      3          -9999      0  
1357113603 1357113602             1     0  -9999          -9999      1  
1357113604 1357113604             1     0  -9999          -9999      2