简体   繁体   English

如何修改熊猫数据框?

[英]How to modify the pandas dataframe?

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

This is my simple dataframe which I try to modify using 这是我尝试使用修改的简单数据框

und_data['trend'][und_data[und_data['fakei']==0].index]
1357113602    2
und_data['trend'][und_data[und_data['fakei']==0].index]=3

This doesn't work. 这行不通。 I checked in Wes Mckinsey Book and my method seems to be correct. 我检查了韦斯·麦肯锡书,我的方法似乎是正确的。 Not sure why it's not working. 不知道为什么它不起作用。 Strangely if I type 奇怪的是我打字

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

It gives the output as 3...but doesn't show in the below output. 它的输出为3 ...但在下面的输出中没有显示。 Something too confusing with copies etc? 复印件太混乱了吗? Can anyone explain this intuitively. 任何人都可以直观地解释这一点。 These simply things seem way convoluted in pandas. 这些简单的事情似乎在大熊猫中令人费解。

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

I'm not entirely sure if I'm copying your data in correctly, but I think you want: 我不确定是否要正确复制您的数据,但我认为您需要:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM