用python把数据串成列

2024-09-24 22:24:21 发布

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

Image 1: In the image of the website from where i have extract data. same column

Image 2: Data which have been extract from the website but in one column (no space and column). I want change this data in the same format(table) as it looks in the image 1.

图1:我想要提取/废弃数据的网站图片。你知道吗

图2:我已经成功地从网站上删除了数据,但是没有我想要的。需要有13列(如图1所示),但是所有的数据都被提取到一列(没有空格和列)。我想以与图1中相同的格式(表)更改此数据。你知道吗

期待您的积极回应。你知道吗

EXCEL文件中的数据

点击链接:https://drive.google.com/file/d/1iy63t4N_aBNNM6L2zFHuna5RN-spu-BR/view?usp=sharing


Tags: 文件数据httpscom网站链接格式google
1条回答
网友
1楼 · 发布于 2024-09-24 22:24:21

好的,有分隔符,但在你的屏幕截图,他们是不可见的,我没有费心看excel文件,所以抱歉。你知道吗

import xlrd
import pandas as pd

wb = xlrd.open_workbook("data.xlsx")
sh = wb.sheet_by_index(0)

# you have to name your columns by hand because not in the data (I put dummy value inside because lazy)
header = ['A'+str(i) for i in range(0,16)] 
print(header)

data = {}

for row_num in range(sh.nrows):
    row = sh.row_values(row_num)[0].split("\n") # The delimiter is '\n'
    if not len(row) == len(header): #To not have the single column1 value in the data
        print("Couldn't process: ", row)
        continue
    for index,column in enumerate(row):
        data[header[index]] = data.get(header[index],[]) + [column]

df = pd.DataFrame(data)

所以分隔符是'\n',你可以用它分开。你知道吗

你必须自己为我输入的列名我把它命名为A0到A15,因为懒得从截图上写下来。你知道吗

相关问题 更多 >