如何将字符串值转换为数据帧

时间:2019-08-18 08:42:59

标签: python python-3.x

我正在使用Selenium Web驱动程序使用命令通过其X路径从网页中提取表格

table=driver.find_element_by_xpath('//*[@id="ccs_VC_caps_dataTable"]').text

这给了我

FC Ship Method Sort Code Destination Warehouse Ship Option Group Name Effective Start Date Effective End Date Day of the week Start Time End Time(exclusive) Time Zone Constraint Type Unit of Measurement Soft Cap Hard Cap MinTarget Max Penalty % Time
Las2 K_PARCEL_24 2019-07-04 2019-07-04 All Europe/London Cubic Volume Cap CUBIC_METER 61 67
Las2 K_PARCEL_24 2019-07-04 2019-07-04 All Europe/London Package Count Cap 1402 1450
Las2 K_PARCEL_48_LL 2019-07-04 2019-07-04 All Europe/London Cubic Volume Cap CUBIC_METER 16 10
Las2 K_PARCEL_48_LL 2019-07-04 2019-07-04 All Europe/London Package Count Cap 125 77

是字符串类型

我想将其转换为熊猫数据框。 我必须使用beautfifulsoup还是有任何变通办法,可以按其X路径提取表并将其转换为熊猫数据框?

2 个答案:

答案 0 :(得分:0)

使用字符串拆分方法创建DataFrame df = pd.DataFrame([table.split('\ n')]中x的[x.split(''))

答案 1 :(得分:0)

这是跳过标题的方法。

pd.DataFrame([x.split(' ') for i, x in enumerate(table.split('\n')) if i != 0])