Home > database >  How can I make a for loop that uses entire string instead of just one element
How can I make a for loop that uses entire string instead of just one element

Time:11-11

I have a list from a dataframe

print(crypto)

              Ticker       Site                             Date
0           Dogezilla-(DOGEZILLA)-    Hotbit   Fri, 29 Oct 2021 11:19:14  0000
1                    Epanus-(EPS)-    Hotbit   Fri, 29 Oct 2021 10:48:16  0000
2           Gods-Unchained-(GODS)-      OKEX   Fri, 29 Oct 2021 08:43:12  0000
3                       LIQ-(LIQ)-      OKEX   Fri, 29 Oct 2021 08:33:13  0000
4              Samoyedcoin-(SAMO)-      OKEX   Fri, 29 Oct 2021 07:33:11  0000
5                GameZone-(GZONE)-      OKEX   Fri, 29 Oct 2021 07:23:11  0000
6                   Aurory-(AURY)-    Kucoin   Fri, 29 Oct 2021 06:53:12  0000
7             BASIC-Token-(BASIC)-    Kucoin   Fri, 29 Oct 2021 06:43:15  0000
8                   Exeedme-(XED)-    Kucoin   Fri, 29 Oct 2021 06:38:12  0000
9                 Metahero-(HERO)-    Kucoin   Fri, 29 Oct 2021 06:23:11  0000

The list is

Tick = list(crypto['Ticker'])
Tick = re.sub(r'-\(.*?\)-','', str(Tick))


['Dogezilla', 'Epanus', 'Gods-Unchained', 'LIQ', 'Samoyedcoin', 'GameZone', 'Aurory', 'BASIC-Token', 'Exeedme', 'Metahero', 'Smart-Valley', 'Dollar-Factory-Finance', 'YDragon', 'Sushi', 'YooShi', 'Gogeta-Inu', 'Songbird', 'OmiseGO', 'Synthetix-Network-Token', 'Sushi', 'PancakeSwap', 'SHIBA-INU', 'Axie-Infinity-Shards']

If I make a for loop saying something like

[print(item) for item in Tick]

It will give an output that's letter by letter

[
'
D
o
g
e
z
i
l
l
a
'
,
 
'
E
p
a
n
u
s
'

What I want to do is make a for loop that looks like

[cg.get_price(ids=item, vs_currencies='usd') for item in Tick]

However this just goes letter by letter breaking the function. So if I want to have a function that puts each string from the list into the ids input.

CodePudding user response:

Right – given the information that crypto is a Pandas dataframe and crypto['Ticker'] refers to a series in it, you might want to just

crypto['Ticker'] = crypto['Ticker'].replace(r'-\(.*?\)-', '', regex=True)

to fix those ticker symbols in-place.

CodePudding user response:

EDIT: using code from AKX's answer, and building on it:

crypto['Ticker'] = crypto['Ticker'].str.replace(r'-\(.*?\)-', '', regex=True)
crypto['Price'] = crypto['Ticker'].transform(lambda item: cg.get_price(ids=item, vs_currencies='usd'))

I think your code doesn't do what you want it to because you're converting Tick (a list) to a str when you do str(Tick). Instead of

Tick = list(crypto['Ticker'])
Tick = re.sub(r'-\(.*?\)-','', str(Tick))

do

Tick = [i.split('-(')[0] for i in crypto['Ticker']]

and iterate over that.

  • Related