-1

I have pandas dataframe like this:

api_url
"url": "https://apis.asia.pntk.cloud/asiaerState",
"url": "https://apis.asia.pntk.cloud/M6518&=38396885",
"url": "https://apis.asia.pntk.cloud/=38396885"
"url": "https://apis.asia.pntk.cloud/518112A",

In the values there could be ',' at the end. I want to trip all special character, take only the actual api url, so the output dataframe would like this:

api_url
https://apis.asia.pntk.cloud/asiaerState
https://apis.asia.pntk.cloud/M65181150%-48UXM%2CNA&so85
https://apis.asia.pntk.cloud/96885
https://apis.asia.pntk.cloud/5181150%2CC9300-48UXM%2A

How can I achieve this?

Dcook
  • 899
  • 7
  • 32
  • Does this answer your question? [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – SiHa Nov 12 '21 at 11:51

1 Answers1

0

Hi, You can try this one. it will solve your Issue.

d = {'api_url': ['"url": "https://apis.asia.pntk.cloud/asiaerState"',
'"url": "https://apis.asia.pntk.cloud/M65181150%-48UXM%2CNA&solutionId=38396885",',
'"url": "https://apis.asia.pntk.cloud/CNA&solutionId=38396885"',
'"url": "https://apis.asia.pntk.cloud/5181150%2CC9300-48UXM%2CNA"']}
df = pd.DataFrame(data=d)

Input:

                                             api_url
0  "url": "https://apis.asia.pntk.cloud/asiaerState"
1  "url": "https://apis.asia.pntk.cloud/M65181150...
2  "url": "https://apis.asia.pntk.cloud/CNA&solut...
3  "url": "https://apis.asia.pntk.cloud/5181150%2...

Solution:

df['api_url'] = df['api_url'].str.replace('"',"").replace(',',"").str.lstrip("url: ")

Output:

                                             api_url
0           https://apis.asia.pntk.cloud/asiaerState
1  https://apis.asia.pntk.cloud/M65181150%-48UXM%...
2  https://apis.asia.pntk.cloud/CNA&solutionId=38...
3  https://apis.asia.pntk.cloud/5181150%2CC9300-4...