PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] How to split integers that contains strings, such as 73101P

 July 19, 2022     integer, split, string     No comments   

Issue

import pandas as pd
d = {'element': ['COGS', 'Other current assets', 'COGS','COGS', 'Other current assets', 'COGS'], 'account_number': ['5721-ES', '1101', '5721-ESP', '73101L', '73230K', '11106' ]}
df = pd.DataFrame(data=d)
df

I need only the numbers without letters for converting them to numeric values afterwards.

However, I can't split these integers for instance 73101K

df.account_number = df.account_number.apply(lambda x: x.split('-')[0])

Solution

You can use findall() that finds every digit (\d) and then join it together afterwards:

df["account_number"] = (
    df["account_number"].str.findall(r"\d").str.join(sep="").astype(int)
)
print(df)

Prints:

                element  account_number
0                  COGS            5721
1  Other current assets            1101
2                  COGS            5721
3                  COGS           73101
4  Other current assets           73230
5                  COGS           11106


Answered By - Andrej Kesely
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing