将df列中的值转换为True / False(Converting values in a df column to True/False)

系统教程 行业动态 更新时间:2024-06-14 16:55:57
将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 False

This 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'])

更多推荐

本文发布于:2023-04-10 11:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7ddc8a7d74c895ec81dc8c64306c7ee5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   True   df   False   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!