1

I'm pulling Json data from the Binance REST API, after formatting I'm left with the following...

I have a dataframe called Assets with 3 columns [Asset,Amount,Location],

['Asset'] holds ticker names for crypto assets e.g.(ETH,LTC,BNB). However when all or part of that asset has been moved to 'Binance Earn' the strings are returned like this e.g.(LDETH,LDLTC,LDBNB).

['Amount'] can be ignored for now.

['Location'] is initially empty. I'm trying to set the value of ['Location'] to 'Earn' if the string in ['Asset'] includes 'LD'.

This is how far I got, but I can't remember how to apply the change to only the current item, it's been ages since I've used Pandas or for loops. And I'm only able to apply it to the entire column rather than the row iteration.


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

The print statements work correctly, but currently the whole column gets populated with the same value (whichever was last) as you might expect. So (LDETH,HOT,LDBTC) returns ('Earn','Earn','Earn') rather than the desired ('Earn','???','Earn')

Any help would be appreciated...

  • Does this answer your question? [Update row values where certain condition is met in pandas](https://stackoverflow.com/questions/36909977/update-row-values-where-certain-condition-is-met-in-pandas) – Gahan Apr 04 '21 at 19:12

2 Answers2

0

One possible solution:

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)

Note, you should almost never iterate over a pandas dataframe or series.

Sam Szotkowski
  • 344
  • 1
  • 6
0

You could run a lambda in df.apply to check whether 'LD' is in df['Asset']:

df['Location'] = df['Asset'].apply(lambda x: 'Earn' if 'LD' in x else None)
RJ Adriaansen
  • 9,131
  • 2
  • 12
  • 26
  • Is there a way to make it look for 'LD' specifically in the column ['Asset']? Some changes now require me to find a substring of x and if present in the ['Asset'] column apply 'Earn' to the ['Location'] column. –  Apr 06 '21 at 09:04
  • My answer applies the lambda only to the `Asset` column. If you wish to use a different substring just replace 'LD' with the new substring. – RJ Adriaansen Apr 06 '21 at 09:12
  • No I mean i need it to check the entire column...every item. Not just x. There is another instance of the string (without 'LD') inside the same column, and if it finds that it should apply the 'Earn'. –  Apr 06 '21 at 09:26
  • But `x` already represents the entire value of a cell in the `Asset` column. The `lambda` iterates over the column, so it covers every item in the column. `if 'LD' in x` asks whether `LD` is a substring of `x`. To look for multiple substrings you could replace `if 'LD' in x` with `if any(s in x for s in ['LD', 'other_substring'])` – RJ Adriaansen Apr 06 '21 at 09:47