将df列中的值转换为True / False(Converting values in a df column to True/False)
我正在尝试更换“在SP100中的所有股票?” 有真或假。 到目前为止,我有错误的工作,但不知道如何让程序说出该列中存在的任何变量,返回True:
newmerge.replace([np.var=='', np.nan], [True, False]) Company Weight IsinSP100? Symbol AAPL Apple Inc. 3.699828 AAPL MSFT Microsoft 2.686835 MSFT AMZN Amazon.com Inc. 1.901109 AMZN AN AutoNation Inc. 0.013148 False NWS News Corporation 0.007284 False这是另一个df中的一列,其中所有库存都在SP100中,但是当我将它放在replace函数中时它不起作用:
Wiki100Data['SP100']我的预期输出将是“在SP500”列中的变量为“True”的所有股票,而在列中没有变量的股票将返回“False”。
I am trying to replace all stocks in the "Is in SP100?" with true or false. So far I have the false working , but not sure how to get the program to say for any variable that exists in that column, return True:
newmerge.replace([np.var=='', np.nan], [True, False]) Company Weight IsinSP100? Symbol AAPL Apple Inc. 3.699828 AAPL MSFT Microsoft 2.686835 MSFT AMZN Amazon.com Inc. 1.901109 AMZN AN AutoNation Inc. 0.013148 False NWS News Corporation 0.007284 FalseThis is a column in another df with all the stocks that ARE in the SP100, but it doesn't work when I put it in the replace function :
Wiki100Data['SP100']My expected output would be all stocks with variables in the "is in SP500" column to be "True", and stocks without variables in the column to return "False".
最满意答案
df['IsinSP100?'] = df.index.isin(Wiki100Data['SP100']) df['IsinSP100?'] = df.index.isin(Wiki100Data['SP100'])更多推荐
发布评论