Pandas.read\u html仅转换特定列

2024-05-23 23:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个程序来读取站点中的表,并仅将表中的部分列转换为float。你知道吗

此站点表如下所示:

Account   Responsible     Grade
1.0.0     João Da Silva   3,5
1.1.0     Antônio Pereira 2,5
1.2.0     Maria do Céu    4,5
1.2.1     Joana Antunes   5,0

为了做到这一点,我用了美肌和熊猫.read\u html如下所示

import BeautifulSoup as bs
import pandas as pd
############################################################
# This part of the code was voided to simplify my question #
############################################################
soup = bs(page_source,'html.parser')
table = soup.find('table',{'id': 'table_id'})
data = pd.read_html(str(table), encoding = 'utf-8', decimal=",", thousands='.')[0]

当我这样做时,表被转换成我想要的,除了“Account”列,返回的数据帧如下所示:

Index   1       2               3
0       Account Responsible     Grade
1       100     João Da Silva   3.5
2       110     Antônio Pereira 2.5
3       120     Maria do Céu    4.5
4       121     Joana Antunes   5.0

我的想法是保持“Account”列的值与原始表中的值相同,以避免任何未完成的转换,并按原样转换其他表的值(在本例中,它应该是[str,str,float])。你知道吗

Index   1         2               3
0       Account   Responsible     Grade
1       1.0.0     João Da Silva   3.5
2       1.1.0     Antônio Pereira 2.5
3       1.2.0     Maria do Céu    4.5
4       1.2.1     Joana Antunes   5.0

有没有可能的方法来执行这种o转换?你知道吗

感谢所有可能的支持,从现在起和最好的问候。你知道吗


Tags: htmltableaccountdosilvaresponsibledagrade