简体   繁体   中英

How to remove certain character from pandas dataframe

I have pandas dataframe like this:

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",

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

How can I achieve this?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM