简体   繁体   English

Pandas nan 不被视为字符串

[英]Pandas nan not treated as string

I have a csv file containing multiuple crypto currencies like this.我有一个包含多种加密货币的 csv 文件。 Column 2 to 4 is id, symbol, name.第 2 到 4 列是 id、符号、名称。

6201,nano-dogecoin,indc,Nano Dogecoin
6202,nano-shiba-inu,NanoShiba,Nano Shiba Inu
6203,nantrade,nan,NanTrade
6204,naos-finance,naos,NAOS Finance
6205,napoleon-x,npx,Napoleon X

I have a function where i get ids by symbols like this:我有一个函数,我可以通过这样的符号获取 id:

def symbols_to_ids(self, symbols):
    ids = []
    df = pd.read_csv(os.getcwd() + "/Backtester/Results/Misc/allcoins.csv")
    for index, row in df.iterrows():
        for symbol in symbols:
            
            if str(row["symbol"].lower()) == str(symbol.lower()):
                
                ids.append(row["id"])
    return ids

However i get an error because the one of the symbols is nan.但是我收到一个错误,因为其中一个符号是 nan。 I am pretty sure it gets treated as a float, since this error gets thrown when the row symbol is nan:我很确定它会被视为浮点数,因为当行符号为 nan 时会抛出此错误:

 if str(row["symbol"].lower()) == str(symbol.lower()): AttributeError: 'float' object has no attribute 'lower'

I tried to convert it to string, but it does not work.我试图将其转换为字符串,但它不起作用。 I think this could be solved in pandas, but I dont know how.我认为这可以在熊猫中解决,但我不知道如何解决。

You're calling the method before converting.您在转换之前调用该方法。 Move .lower to outside conversion:.lower移动到外部转换:

if str(row["symbol"]).lower() == str(symbol).lower():

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

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