Home > Blockchain >  Replace http from url to https in pandas dataframe
Replace http from url to https in pandas dataframe

Time:04-29

i have a column in dataframe, which contains a few urls For Example before:

| url                | anydata            |
| -------------------| ------------------ |
| https://google.com | anydata            |
| http:/bing.com     | anydata            |
| https:/yahoo.com   | anydata            |

how i can replace all http to https? For example after:


| url                | anydata            |
| -------------------| ------------------ |
| https://google.com | anydata            | 
| https:/bing.com    | anydata            |
| https:/yahoo.com   | anydata            |

CodePudding user response:

Use a regular expression:

df['url'] = df['url'].str.replace(r'^http\b', 'https', regex=True)

Output:

>>> df
                  url  anydata
0  https://google.com  anydata
1     https:/bing.com  anydata
2    https:/yahoo.com  anydata
  • Related