Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 比较列值_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 比较列值

Python 3.x 比较列值,python-3.x,pandas,Python 3.x,Pandas,我创建了两列,如下所示: df_o['a'] = (df['sell'] == 1).resample(interval).sum().astype(int) df_o['b'] = (df['sell'] == 0).resample(interval).sum().astype(int) df_o[df_o['ratioab'] == np.inf] = 0 我希望计算这两列之间的比率,即: df_o['ratioab'] = df_o['a'] / df_o['b'] 这是可行的,但

我创建了两列,如下所示:

df_o['a'] = (df['sell'] == 1).resample(interval).sum().astype(int)
df_o['b'] = (df['sell'] == 0).resample(interval).sum().astype(int)
df_o[df_o['ratioab'] == np.inf] = 0
我希望计算这两列之间的比率,即:

df_o['ratioab'] = df_o['a'] / df_o['b']
这是可行的,但我无法找到一种方法来避免熊猫等于Inf的
x/0
0/x
。在这种情况下,我希望设置
df_o['ratioab']=0

这不起作用:

if (0 == df_o['a']).all() | (0 == df_o['b']).all():
    df_ohlc['ratioab'] = 0

一种可能的解决办法是:

df_o['ratioab'] = (df_o['a'] / df_o['b']).replace(np.inf, 0)

您也可以使用类似以下内容:

df_o['a'] = (df['sell'] == 1).resample(interval).sum().astype(int)
df_o['b'] = (df['sell'] == 0).resample(interval).sum().astype(int)
df_o[df_o['ratioab'] == np.inf] = 0

是否可以创建一些示例数据?因为似乎总是有
x/0
0/x
可以,但现在不行。很好,谢谢!我不知道如何将列数据与python类型tho进行比较,这仍然困扰着我。@krizajb-我尝试查找一些示例数据,但失败;)@krizajb-如果想要comapre列
a
,最好是
df_o['a']==0
,但你的代码也能工作。你的代码在与
运算符一起使用时会出现问题,即
df_o['a']==0或(|)df_o['b']==0
@krizajb-你认为
掩码=(df_o['a','b']=0)。所有(axis=1)
(掩码,0,df_o['col'])