鱼C论坛

 找回密码
 立即注册
查看: 1507|回复: 2

[已解决]pandas分割字符串

[复制链接]
发表于 2021-8-8 21:16:24 | 显示全部楼层 |阅读模式
20鱼币
requtes爬取天气后获取如下图dataframe,要求将【b】列中数据分割成【天气,最高温度,最低温度】三列。
  1. import bs4
  2. import requests
  3. import numpy as np
  4. import pandas as pd

  5. url = 'http://m.apporid.com/xian/1yue.html'
  6. headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, '
  7.                          'like Gecko) Chrome/91.0.4472.164 Mobile Safari/537.36',
  8.            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,'
  9.                      '*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 ',
  10.            }
  11. res = requests.get(url, headers=headers)
  12. temperature = []

  13. soup = bs4.BeautifulSoup(res.text, "html.parser")
  14. targets = soup.find("ul", class_="tqullist tqicon28x20")
  15. targets = targets.find_all('li')
  16. for each in targets:
  17.     temperature.append(each.text.split())

  18. t1 = np.array(temperature).reshape(len(temperature), 2)
  19. df = pd.DataFrame(t1, columns=["日期", "b"])
  20. print(df)
复制代码

图片
01.png
不要按下标索引分组方法。
最佳答案
2021-8-8 21:16:25
df.b.str.split('/', expand=True)  #通常可以。 可你这前面中文天气和低温连在了一起咋分都不方便。。看看有没有正则高手,可以帮你掰开,哈哈

话说,你直接爬取的时候,分好3列,不好些吗,比如
  1. import bs4, requests, re
  2. import numpy as np
  3. import pandas as pd

  4. url = 'http://m.apporid.com/xian/1yue.html'
  5. res = requests.get(url,)
  6. temperature = []
  7. soup = bs4.BeautifulSoup(res.text, "html.parser")
  8. targets = soup.find("ul", class_="tqullist tqicon28x20")
  9. dt = targets.find_all('b')
  10. tq = targets.find_all('div')
  11. temp = targets.find_all('span')
  12. for d, i, j in zip(dt, tq, temp):
  13.     temperature.append([d.text, i.text.strip(), j.text])
  14.     # print([d.text, i.text.strip(), j.text])

  15. t1 = np.array(temperature).reshape(len(temperature), 3)
  16. df = pd.DataFrame(t1, columns=["日期", "天气", "温度"])
  17. print(df)
复制代码

最佳答案

查看完整内容

df.b.str.split('/', expand=True) #通常可以。 可你这前面中文天气和低温连在了一起咋分都不方便。。看看有没有正则高手,可以帮你掰开,哈哈 话说,你直接爬取的时候,分好3列,不好些吗,比如
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 21:16:25 | 显示全部楼层    本楼为最佳答案   
df.b.str.split('/', expand=True)  #通常可以。 可你这前面中文天气和低温连在了一起咋分都不方便。。看看有没有正则高手,可以帮你掰开,哈哈

话说,你直接爬取的时候,分好3列,不好些吗,比如
  1. import bs4, requests, re
  2. import numpy as np
  3. import pandas as pd

  4. url = 'http://m.apporid.com/xian/1yue.html'
  5. res = requests.get(url,)
  6. temperature = []
  7. soup = bs4.BeautifulSoup(res.text, "html.parser")
  8. targets = soup.find("ul", class_="tqullist tqicon28x20")
  9. dt = targets.find_all('b')
  10. tq = targets.find_all('div')
  11. temp = targets.find_all('span')
  12. for d, i, j in zip(dt, tq, temp):
  13.     temperature.append([d.text, i.text.strip(), j.text])
  14.     # print([d.text, i.text.strip(), j.text])

  15. t1 = np.array(temperature).reshape(len(temperature), 3)
  16. df = pd.DataFrame(t1, columns=["日期", "天气", "温度"])
  17. print(df)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-9 07:46:05 From FishC Mobile | 显示全部楼层
用正则吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-20 14:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表