简体   繁体   English

将带有逗号的数据框中的列转换为数字数据以进行绘图

[英]Convert columns in dataframe with comas into numeric data to plotting

I'm new in the world of plotting in Python I started learning today doing a mini project by my own, I tried to scrape data and represent here's my code:我是 Python 绘图领域的新手我今天开始学习自己做一个小项目,我尝试抓取数据并表示这是我的代码:

import requests
import pandas as pd
from pandas import DataFrame
import numpy as np
import bs4
from bs4 import BeautifulSoup
import matplotlib.pyplot as plot

# Getting the HTML page
URL = "https://www.worldometers.info/coronavirus/#countries"
pag_html = requests.get(URL).text

# Extracting data with BeautifulSoup.
soup = BeautifulSoup(pag_html, 'html.parser')

tabla = soup.find("table", id="main_table_countries_today")

datos_tabla = tabla.tbody.find_all("tr")

Lista = []

for x in range(len(datos_tabla)):
  values = [j.string for j in datos_tabla[x].find_all('td')]
  Lista.append(values)


df = pd.DataFrame(Lista).iloc[7: , 1:9]
nombre_columna = ["Pais", "Casos totales", "Nuevos Casos", "Muertes totales", "Nuevas Muertes", "Total Recuperados", "Nuevos Recuperados", "Activos"]
df.columns = nombre_columna



df.plot(x="Pais", y="Casos totales", kind ="barh")
plot.show()

The error it's giving me is: "TypeError: no numeric data to plot" I understand that this error is because the column "Casos totales" is a string not a float.它给我的错误是:“类型错误:没有要绘制的数字数据”我知道这个错误是因为“Casos totales”列是一个字符串而不是一个浮点数。 I tried to convert the columns of my Dataframe into floats, but there's no way I got error from everywhere.我试图将我的 Dataframe 的列转换为浮点数,但我无法从任何地方得到错误。 Does anyone have any idea how can I represent my DataFrame?有谁知道我如何表示我的 DataFrame?

Thanks.谢谢。

After running the script, as you say the column "Casos Totales" is being interpreted as string due to the commas in the values.运行脚本后,正如您所说,由于值中的逗号,列“Casos Totales”被解释为字符串。 You can change this using .str.replace(',','') and then .astype(float) , right after renaming the column names in your dataframe:您可以使用.str.replace(',','')然后.astype(float)更改它,在重命名数据.astype(float)的列名之后:

df['Casos totales'] = df['Casos totales'].str.replace(',','').astype(float)


df.plot(x="Pais", y="Casos totales", kind ="barh")
plot.show()

And this plots the graph (although the visualization is quite poor, but that's another story)这绘制了图形(虽然可视化效果很差,但那是另一回事了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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