Home > Enterprise >  How to remove certain character from pandas dataframe
How to remove certain character from pandas dataframe

Time:11-13

I have pandas dataframe like this:

api_url
"url": "https://apis.asia.pntk.cloud/asiaerState",
"url": "https://apis.asia.pntk.cloud/M65181150%-48UXM,NA&solutionId=38396885",
"url": "https://apis.asia.pntk.cloud/CNA&solutionId=38396885"
"url": "https://apis.asia.pntk.cloud/5181150,C9300-48UXM,NA",

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,NA&solutionId=38396885
https://apis.asia.pntk.cloud/CNA&solutionId=38396885
https://apis.asia.pntk.cloud/5181150,C9300-48UXM,NA

How can I achieve this?

CodePudding user response:

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,NA&solutionId=38396885",',
'"url": "https://apis.asia.pntk.cloud/CNA&solutionId=38396885"',
'"url": "https://apis.asia.pntk.cloud/5181150,C9300-48UXM,NA"']}
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,C9300-4...
  • Related