Pandas For 循环,如果列中存在字符串,则列值 = X

Pandas For Loop, If String Is Present In ColumnA Then ColumnB Value = X

我正在从 Binance REST API 中提取 Json 数据,格式化后我得到以下内容...

我有一个名为 Assets 的数据框,其中包含 3 列 [Asset,Amount,Location],

['Asset'] 持有加密资产的代码名称,例如(ETH、LTC、BNB)。 然而,当该资产的全部或部分已移动到 'Binance Earn' 时,字符串将像这样返回,例如(LDETH,LDLTC,LDBNB)。

['Amount']暂时可以忽略

['Location'] 最初是空的。 如果 ['Asset'] 中的字符串包含 'LD'.

,我正在尝试将 ['Location'] 的值设置为 'Earn'

这是我的进展,但我不记得如何仅将更改应用到当前项目,自从我使用 Pandas 或 for 循环以来已经很久了。 而且我只能将它应用于整个列而不是行迭代。


for Row in Assets['Asset']:
    if Row.find('LD') == 0:
        print('Earn')
        Assets['Location'] = 'Earn' # <----How to apply this to current row only
    else:
        print('???')
        Assets['Location'] = '???' # <----How to apply this to current row only

打印语句工作正常,但目前整个列都填充了与您预期相同的值(以最后一个为准)。 所以 (LDETH,HOT,LDBTC) returns ('Earn','Earn','Earn') 而不是所需的 ('Earn','???','Earn')

任何帮助将不胜感激...

一个可能的解决方案:

def get_loc(row):
    asset = row['Asset']
    if asset.find('LD') == 0:
        print('Earn')
        return 'Earn'
    print('???')
    return '???'

Assets['Location'] = Assets.apply(get_loc, axis=1)

请注意,您几乎不应该迭代 pandas 数据框或系列。

您可以 运行 df.apply 中的 lambda 来检查 'LD' 是否在 df['Asset'] 中:

df['Location'] = df['Asset'].apply(lambda x: 'Earn' if 'LD' in x else None)

np.where() 适合这里。如果 AssetLD 开头,则 return Earn,否则 return ???:

Assets['Location'] = np.where(Assets['Asset'].str.startswith('LD'), 'Earn', '???')