1

I have a dataframe with 1 column containing the amount bought of a particular crypto

df['Amount'] = 200.20356AVAX

I would like to split this object into 2 columns:

  • 1 containing the number --> df['Quantity'] = 200.20356
  • 1 containing the word --> df['Asset'] = AVAX
invesTIPS
  • 33
  • 1
  • 4
  • 3
    I doubt that line of code is valid Python... are you sure it's not a string? – Marco Bonelli Nov 08 '21 at 22:04
  • Have you _tried something_? There are many [options](https://stackoverflow.com/questions/430079/how-to-split-strings-into-text-and-number). – msanford Nov 08 '21 at 22:06
  • Does this answer your question? [How to split strings into text and number?](https://stackoverflow.com/questions/430079/how-to-split-strings-into-text-and-number) – Jonathan Scholbach Nov 08 '21 at 22:09
  • parse the string '200.20356AVAX' using a regular expression to separate digits from alpha – Golden Lion Nov 08 '21 at 22:09
  • As stated below by @Corralien df = df.join(df['Amount'].str.extract('([^A-Z]+)([A-Z]+)') \ .rename(columns={0: 'Quantity', 1: 'Asset'})) – invesTIPS Nov 08 '21 at 22:24

2 Answers2

1

Use str.extract:

df = pd.DataFrame({'Amount': ['200.20356AVAX']})

df = df.join(df['Amount'].str.extract('([^A-Z]+)([A-Z]+)') \
                         .rename(columns={0: 'Quantity', 1: 'Asset'}))

# OR, proposed by @mozway (more efficient)

df = df.join(df['Amount'].str.extract('(?P<Quantity>[^A-Z]+)(?P<Asset>[A-Z]+)'))

Output:

>>> df
          Amount   Quantity Asset
0  200.20356AVAX  200.20356  AVAX
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

use a regular expression in an dataframe apply

txt="200.20356AVAX"
pattern=r"\d+\.*\d*"
print(re.findall(pattern,txt))

output:

['200.20356']
Golden Lion
  • 3,840
  • 2
  • 26
  • 35