import requestsfrom bs4 import BeautifulSoupimport pandas as pdurl = 'https://www.gk100.com/read_16892539.htm'header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15"}r = requests.get(url, headers=header)r.encoding = r.apparent_encodingsoup = BeautifulSoup(r.text, features="lxml")w1 = soup.find_all("td")result = w1df = pd.DataFrame(result)df1 = df[2:]for index in df1: d1 = df1[df1.index%2==0] d2 = df1[df1.index%2!=0] d1 = d1.reset_index(drop=True) d2 = d2.reset_index(drop=True) df2 = pd.concat([d1,d2], axis=1)# 排序,按照第二列的元素进行降序排列df2 = df2.sort_values(by='人数/万', ascending=False)title = ['省份', '人数/万']df2.columns = titleprint(df2)将这个程序中人数/万的这列转化成数字
时间: 2024-04-02 18:37:28 浏览: 163
您可以使用pandas中的apply函数来将“人数/万”列转换为数字类型,例如:
```
df2['人数/万'] = df2['人数/万'].apply(lambda x: float(x.replace(',', '')))
```
这将使用lambda函数将每个值中的逗号删除并将其转换为浮点数类型。然后将修改后的结果存储回原始DataFrame中的“人数/万”列。
阅读全文